tmux.plugin.zsh 5.7 KB

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