heydelicias

tmux-projects.sh

Jul 15th, 2025
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.45 KB | None | 0 0
  1. #!/bin/bash
  2. # Select a project from a fed list of projects
  3. # selected=$(cat ~/.tmux-projects-list | fzf)
  4. selected=$(find ~/Desktop ~/Downloads ~/Projects -maxdepth 5 -type d | fzf )
  5.  
  6. # Does tilda substitution for support with different shells
  7. fix_selected=${selected/#~/$HOME}
  8.  
  9. # Bail if nothing is selected
  10. if [[ -z $selected ]]; then
  11.     exit 0
  12. fi
  13.  
  14. # Bail if selection does not exists
  15. if [[ ! -d $fix_selected ]]; then
  16.     tmux neww bash -c "echo -n \"No such file or directory: $selected\" & while [ : ]; do sleep 1; done"
  17.     exit 0
  18. fi
  19.  
  20. # Changes directory to the project directory
  21. # so that git and telescope work fine
  22. cd $fix_selected
  23.  
  24. # Asks for a file to be opened directly
  25. file=$(fzf --bind enter:accept-or-print-query)
  26.  
  27. # If the file does not exists it will create the file
  28. # Accidentally created files can be easily managed with git. ( e.g. Typos )
  29. if [[ ! -f "$fix_selected/$file" ]]; then
  30.     touch "$fix_selected/$file"
  31. fi
  32.  
  33.  
  34. selected_name=$(basename $fix_selected)
  35. tmux_running=$(pgrep tmux)
  36.  
  37. if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
  38.     tmux new-session $fix_selected -c $fix_selected
  39.     exit 0
  40. fi
  41.  
  42. if ! tmux has-session -t=$selected_name 2> /dev/null; then
  43.     tmux new-session -ds $selected_name -c $fix_selected
  44. fi
  45.  
  46. tmux switch-client -t $selected_name
  47.  
  48. # Opens a new tmux window with neovim editing the selected file
  49. tmux neww -n $(basename $selected) -t $selected_name bash -c "nvim $selected/$file"
  50.  
Advertisement
Add Comment
Please, Sign In to add comment