tmux.plugin.zsh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. # CONFIGURATION VARIABLES
  13. # Automatically start tmux
  14. : ${ZSH_TMUX_AUTOSTART:=false}
  15. # Only autostart once. If set to false, tmux will attempt to
  16. # autostart every time your zsh configs are reloaded.
  17. : ${ZSH_TMUX_AUTOSTART_ONCE:=true}
  18. # Automatically connect to a previous session if it exists
  19. : ${ZSH_TMUX_AUTOCONNECT:=true}
  20. # Automatically close the terminal when tmux exits
  21. : ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
  22. # Set term to screen or screen-256color based on current terminal support
  23. : ${ZSH_TMUX_FIXTERM:=true}
  24. # Set '-CC' option for iTerm2 tmux integration
  25. : ${ZSH_TMUX_ITERM2:=false}
  26. # The TERM to use for non-256 color terminals.
  27. # Tmux states this should be screen, but you may need to change it on
  28. # systems without the proper terminfo
  29. : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
  30. # The TERM to use for 256 color terminals.
  31. # Tmux states this should be screen-256color, but you may need to change it on
  32. # systems without the proper terminfo
  33. : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
  34. # Set the configuration path
  35. : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
  36. # Determine if the terminal supports 256 colors
  37. if [[ $terminfo[colors] == 256 ]]; then
  38. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
  39. else
  40. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
  41. fi
  42. # Set the correct local config file to use.
  43. if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
  44. export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
  45. else
  46. export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
  47. fi
  48. # Wrapper function for tmux.
  49. function _zsh_tmux_plugin_run() {
  50. if [[ -n "$@" ]]; then
  51. command tmux "$@"
  52. return $?
  53. fi
  54. local -a tmux_cmd
  55. tmux_cmd=(command tmux)
  56. [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
  57. # Try to connect to an existing session.
  58. [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
  59. # If failed, just run tmux, fixing the TERM variable if requested.
  60. if [[ $? -ne 0 ]]; then
  61. if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
  62. tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
  63. elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
  64. tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
  65. fi
  66. $tmux_cmd new-session
  67. fi
  68. if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
  69. exit
  70. fi
  71. }
  72. # Use the completions for tmux for our function
  73. compdef _tmux _zsh_tmux_plugin_run
  74. # Alias tmux to our wrapper function.
  75. alias tmux=_zsh_tmux_plugin_run
  76. # Autostart if not already in tmux and enabled.
  77. if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
  78. # Actually don't autostart if we already did and multiple autostarts are disabled.
  79. if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
  80. export ZSH_TMUX_AUTOSTARTED=true
  81. _zsh_tmux_plugin_run
  82. fi
  83. fi