tmux.zsh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. if [ -z "$TMUX" ]; then
  2. # If we're in SSH
  3. if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
  4. echo "Welcome via SSH..."
  5. # If we're in a TTY without X
  6. elif [ -z "$DISPLAY" ]; then
  7. # Optional X start code commented out
  8. # echo "Start X? ..."
  9. # startx
  10. # Regular terminal with no tmux
  11. else
  12. tmux_sessions=$(tmux ls 2>/dev/null)
  13. tmux_status=$?
  14. if [ $tmux_status -ne 0 ]; then
  15. echo "Press any key to cancel tmux autostart..."
  16. read -t 0.7 -n 1 -s -r
  17. if [ $? -ne 0 ]; then
  18. tmux new -s default
  19. fi
  20. else
  21. tmux attach -t default || tmux new -s default
  22. fi
  23. fi
  24. fi
  25. function tmuxLauncher {
  26. local selected_dir full_path session_name
  27. selected_dir=$(fd --base-directory ~/Dev --search-path . -t d -d 2 | fzf)
  28. [[ -z "$selected_dir" ]] && { zle reset-prompt; return }
  29. selected_dir="${selected_dir#./}"
  30. selected_dir="${selected_dir%/}"
  31. full_path="$HOME/Dev/$selected_dir"
  32. session_name="${selected_dir//\//-}"
  33. session_name="${session_name%-}"
  34. if [[ -n "$TMUX" ]]; then
  35. tmux new-window -n "$session_name" -c "$full_path"
  36. zle reset-prompt
  37. else
  38. echo "tmux new-session -s '$session_name' -c '$full_path'" > /tmp/tmux_cmd
  39. zle accept-line
  40. BUFFER="source /tmp/tmux_cmd && rm /tmp/tmux_cmd"
  41. zle accept-line
  42. fi
  43. }
  44. zle -N tmuxLauncher
  45. bindkey "^t" tmuxLauncher