tmux.plugin.zsh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. if ! (( $+commands[tmux] )); then
  2. print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
  3. return 1
  4. fi
  5. # CONFIGURATION VARIABLES
  6. # Automatically start tmux
  7. : ${ZSH_TMUX_AUTOSTART:=false}
  8. # Only autostart once. If set to false, tmux will attempt to
  9. # autostart every time your zsh configs are reloaded.
  10. : ${ZSH_TMUX_AUTOSTART_ONCE:=true}
  11. # Automatically connect to a previous session if it exists
  12. : ${ZSH_TMUX_AUTOCONNECT:=true}
  13. # Automatically close the terminal when tmux exits
  14. : ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
  15. # Automatically name the new session based on the basename of PWD
  16. : ${ZSH_TMUX_AUTONAME_SESSION:=false}
  17. # Automatically pick up tmux environments
  18. : ${ZSH_TMUX_AUTOREFRESH:=true}
  19. # Set term to screen or screen-256color based on current terminal support
  20. : ${ZSH_TMUX_DETACHED:=false}
  21. # Set detached mode
  22. : ${ZSH_TMUX_FIXTERM:=true}
  23. # Set '-CC' option for iTerm2 tmux integration
  24. : ${ZSH_TMUX_ITERM2:=false}
  25. # The TERM to use for non-256 color terminals.
  26. # Tmux states this should be tmux|screen, but you may need to change it on
  27. # systems without the proper terminfo
  28. if [[ -e /usr/share/terminfo/t/tmux ]]; then
  29. : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=tmux}
  30. else
  31. : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
  32. fi
  33. # The TERM to use for 256 color terminals.
  34. # Tmux states this should be (tmux|screen)-256color, but you may need to change it on
  35. # systems without the proper terminfo
  36. if [[ -e /usr/share/terminfo/t/tmux-256color ]]; then
  37. : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=tmux-256color}
  38. else
  39. : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
  40. fi
  41. # Set the configuration path
  42. if [[ -e $HOME/.tmux.conf ]]; then
  43. : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
  44. elif [[ -e ${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf ]]; then
  45. : ${ZSH_TMUX_CONFIG:=${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf}
  46. else
  47. : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
  48. fi
  49. # Set -u option to support unicode
  50. : ${ZSH_TMUX_UNICODE:=false}
  51. # ALIASES
  52. function _build_tmux_alias {
  53. setopt localoptions no_rc_expand_param
  54. eval "function $1 {
  55. if [[ -z \$1 ]] || [[ \${1:0:1} == '-' ]]; then
  56. tmux $2 \"\$@\"
  57. else
  58. tmux $2 $3 \"\$@\"
  59. fi
  60. }"
  61. local f s
  62. f="_omz_tmux_alias_${1}"
  63. s=(${(z)2})
  64. eval "function ${f}() {
  65. shift words;
  66. words=(tmux ${@:2} \$words);
  67. ((CURRENT+=${#s[@]}+1))
  68. _tmux
  69. }"
  70. compdef "$f" "$1"
  71. }
  72. alias tksv='tmux kill-server'
  73. alias tl='tmux list-sessions'
  74. alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
  75. _build_tmux_alias "ta" "attach" "-t"
  76. _build_tmux_alias "tad" "attach -d" "-t"
  77. _build_tmux_alias "ts" "new-session" "-s"
  78. _build_tmux_alias "tkss" "kill-session" "-t"
  79. unfunction _build_tmux_alias
  80. # Determine if the terminal supports 256 colors
  81. if [[ $terminfo[colors] == 256 ]]; then
  82. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
  83. else
  84. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
  85. fi
  86. # Handle $0 according to the standard:
  87. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  88. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  89. 0="${${(M)0:#/*}:-$PWD/$0}"
  90. # Set the correct local config file to use.
  91. if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
  92. export ZSH_TMUX_CONFIG
  93. export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
  94. else
  95. export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
  96. fi
  97. # Wrapper function for tmux.
  98. function _zsh_tmux_plugin_run() {
  99. if [[ -n "$@" ]]; then
  100. command tmux "$@"
  101. return $?
  102. fi
  103. local -a tmux_cmd
  104. tmux_cmd=(command tmux)
  105. [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
  106. [[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
  107. local _detached=""
  108. [[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d"
  109. local session_name
  110. if [[ "$ZSH_TMUX_AUTONAME_SESSION" == "true" ]]; then
  111. # Name the session after the basename of the current directory
  112. session_name=${PWD##*/}
  113. # If the current directory is the home directory, name it 'HOME'
  114. [[ "$PWD" == "$HOME" ]] && session_name="HOME"
  115. # If the current directory is the root directory, name it 'ROOT'
  116. [[ "$PWD" == "/" ]] && session_name="ROOT"
  117. else
  118. session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
  119. fi
  120. # Try to connect to an existing session.
  121. if [[ -n "$session_name" ]]; then
  122. [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
  123. else
  124. [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached
  125. fi
  126. # If failed, just run tmux, fixing the TERM variable if requested.
  127. if [[ $? -ne 0 ]]; then
  128. if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
  129. tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
  130. elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
  131. tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
  132. fi
  133. if [[ -n "$session_name" ]]; then
  134. $tmux_cmd new-session -s "$session_name"
  135. else
  136. $tmux_cmd new-session
  137. fi
  138. fi
  139. if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
  140. exit
  141. fi
  142. }
  143. # Refresh tmux environment variables.
  144. function _zsh_tmux_plugin_preexec()
  145. {
  146. local -a tmux_cmd
  147. tmux_cmd=(command tmux)
  148. eval $($tmux_cmd show-environment -s)
  149. }
  150. # Use the completions for tmux for our function
  151. compdef _tmux _zsh_tmux_plugin_run
  152. # Alias tmux to our wrapper function.
  153. alias tmux=_zsh_tmux_plugin_run
  154. function _tmux_directory_session() {
  155. # current directory without leading path
  156. local dir=${PWD##*/}
  157. # md5 hash for the full working directory path
  158. local md5=$(printf '%s' "$PWD" | md5sum | cut -d ' ' -f 1)
  159. # human friendly unique session name for this directory
  160. local session_name="${dir}-${md5:0:6}"
  161. # create or attach to the session
  162. tmux new -As "$session_name"
  163. }
  164. alias tds=_tmux_directory_session
  165. # Autostart if not already in tmux and enabled.
  166. if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" ]]; then
  167. # Actually don't autostart if we already did and multiple autostarts are disabled.
  168. if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
  169. export ZSH_TMUX_AUTOSTARTED=true
  170. _zsh_tmux_plugin_run
  171. fi
  172. fi
  173. # Automatically refresh tmux environments if tmux is running.
  174. if [[ -n "$TMUX" && "$ZSH_TMUX_AUTOREFRESH" == "true" ]] && tmux ls >/dev/null 2>/dev/null; then
  175. autoload -U add-zsh-hook
  176. add-zsh-hook preexec _zsh_tmux_plugin_preexec
  177. fi