tmux.plugin.zsh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # ALIASES
  6. alias ta='tmux attach -t'
  7. alias tad='tmux attach -d -t'
  8. alias ts='tmux new-session -s'
  9. alias tl='tmux list-sessions'
  10. alias tksv='tmux kill-server'
  11. alias tkss='tmux kill-session -t'
  12. alias tmuxconf='$EDITOR ~/.tmux.conf'
  13. # CONFIGURATION VARIABLES
  14. # Automatically start tmux
  15. : ${ZSH_TMUX_AUTOSTART:=false}
  16. # Only autostart once. If set to false, tmux will attempt to
  17. # autostart every time your zsh configs are reloaded.
  18. : ${ZSH_TMUX_AUTOSTART_ONCE:=true}
  19. # Automatically connect to a previous session if it exists
  20. : ${ZSH_TMUX_AUTOCONNECT:=true}
  21. # Automatically close the terminal when tmux exits
  22. : ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
  23. # Set term to screen or screen-256color based on current terminal support
  24. : ${ZSH_TMUX_FIXTERM:=true}
  25. # Set '-CC' option for iTerm2 tmux integration
  26. : ${ZSH_TMUX_ITERM2:=false}
  27. # The TERM to use for non-256 color terminals.
  28. # Tmux states this should be screen, but you may need to change it on
  29. # systems without the proper terminfo
  30. : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
  31. # The TERM to use for 256 color terminals.
  32. # Tmux states this should be screen-256color, but you may need to change it on
  33. # systems without the proper terminfo
  34. : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
  35. # Set the configuration path
  36. : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
  37. # Set -u option to support unicode
  38. : ${ZSH_TMUX_UNICODE:=false}
  39. # Determine if the terminal supports 256 colors
  40. if [[ $terminfo[colors] == 256 ]]; then
  41. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
  42. else
  43. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
  44. fi
  45. # Handle $0 according to the standard:
  46. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  47. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  48. 0="${${(M)0:#/*}:-$PWD/$0}"
  49. # Set the correct local config file to use.
  50. if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
  51. export ZSH_TMUX_CONFIG
  52. export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
  53. else
  54. export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
  55. fi
  56. # Wrapper function for tmux.
  57. function _zsh_tmux_plugin_run() {
  58. if [[ -n "$@" ]]; then
  59. command tmux "$@"
  60. return $?
  61. fi
  62. local -a tmux_cmd
  63. tmux_cmd=(command tmux)
  64. [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
  65. [[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
  66. # Try to connect to an existing session.
  67. [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
  68. # If failed, just run tmux, fixing the TERM variable if requested.
  69. if [[ $? -ne 0 ]]; then
  70. if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
  71. tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
  72. elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
  73. tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
  74. fi
  75. if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
  76. $tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_SESSION_NAME
  77. else
  78. $tmux_cmd new-session
  79. fi
  80. fi
  81. if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
  82. exit
  83. fi
  84. }
  85. # Use the completions for tmux for our function
  86. compdef _tmux _zsh_tmux_plugin_run
  87. # Alias tmux to our wrapper function.
  88. alias tmux=_zsh_tmux_plugin_run
  89. # Autostart if not already in tmux and enabled.
  90. if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
  91. # Actually don't autostart if we already did and multiple autostarts are disabled.
  92. if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
  93. export ZSH_TMUX_AUTOSTARTED=true
  94. _zsh_tmux_plugin_run
  95. fi
  96. fi