tmux.plugin.zsh 4.0 KB

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