tmux.plugin.zsh 4.7 KB

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