tmux.plugin.zsh 5.8 KB

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