tmux.plugin.zsh 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #
  2. # Aliases
  3. #
  4. alias ta='tmux attach -t'
  5. alias ts='tmux new-session -s'
  6. alias tl='tmux list-sessions'
  7. alias tksv='tmux kill-server'
  8. alias tkss='tmux kill-session -t'
  9. # Only run if tmux is actually installed
  10. if which tmux &> /dev/null
  11. then
  12. # Configuration variables
  13. #
  14. # Automatically start tmux
  15. [[ -n "$ZSH_TMUX_AUTOSTART" ]] || 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. [[ -n "$ZSH_TMUX_AUTOSTART_ONCE" ]] || ZSH_TMUX_AUTOSTART_ONCE=true
  19. # Automatically connect to a previous session if it exists
  20. [[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true
  21. # Automatically close the terminal when tmux exits
  22. [[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART
  23. # Set term to screen or screen-256color based on current terminal support
  24. [[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true
  25. # Set '-CC' option for iTerm2 tmux integration
  26. [[ -n "$ZSH_TMUX_ITERM2" ]] || 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. [[ -n "$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR" ]] || 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. [[ -n "$ZSH_TMUX_FIXTERM_WITH_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITH_256COLOR="screen-256color"
  35. # Get the absolute path to the current directory
  36. local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
  37. # Determine if the terminal supports 256 colors
  38. if [[ `tput colors` == "256" ]]
  39. then
  40. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
  41. else
  42. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
  43. fi
  44. # Set the correct local config file to use.
  45. if [[ "$ZSH_TMUX_ITERM2" == "false" ]] && [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
  46. then
  47. #use this when they have a ~/.tmux.conf
  48. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.extra.conf"
  49. else
  50. #use this when they don't have a ~/.tmux.conf
  51. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.only.conf"
  52. fi
  53. # Wrapper function for tmux.
  54. function _zsh_tmux_plugin_run()
  55. {
  56. # We have other arguments, just run them
  57. if [[ -n "$@" ]]
  58. then
  59. \tmux $@
  60. # Try to connect to an existing session.
  61. elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
  62. then
  63. \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` attach || \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG` new-session
  64. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  65. # Just run tmux, fixing the TERM variable if requested.
  66. else
  67. \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG`
  68. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  69. fi
  70. }
  71. # Use the completions for tmux for our function
  72. compdef _tmux _zsh_tmux_plugin_run
  73. # Alias tmux to our wrapper function.
  74. alias tmux=_zsh_tmux_plugin_run
  75. # Autostart if not already in tmux and enabled.
  76. if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
  77. 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" ]]
  80. then
  81. export ZSH_TMUX_AUTOSTARTED=true
  82. _zsh_tmux_plugin_run
  83. fi
  84. fi
  85. else
  86. print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin."
  87. fi