tmux.plugin.zsh 5.0 KB

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