tmux.plugin.zsh 5.5 KB

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