tmux.plugin.zsh 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # Aliases
  3. #
  4. alias ta='tmux attach -t'
  5. alias tad='tmux attach -d -t'
  6. alias ts='tmux new-session -s'
  7. alias tl='tmux list-sessions'
  8. alias tksv='tmux kill-server'
  9. alias tkss='tmux kill-session -t'
  10. # Only run if tmux is actually installed
  11. if which tmux &> /dev/null
  12. then
  13. # Configuration variables
  14. #
  15. # Automatically start tmux
  16. [[ -n "$ZSH_TMUX_AUTOSTART" ]] || ZSH_TMUX_AUTOSTART=false
  17. # Only autostart once. If set to false, tmux will attempt to
  18. # autostart every time your zsh configs are reloaded.
  19. [[ -n "$ZSH_TMUX_AUTOSTART_ONCE" ]] || ZSH_TMUX_AUTOSTART_ONCE=true
  20. # Automatically connect to a previous session if it exists
  21. [[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true
  22. # Automatically close the terminal when tmux exits
  23. [[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART
  24. # Set term to screen or screen-256color based on current terminal support
  25. [[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true
  26. # Set '-CC' option for iTerm2 tmux integration
  27. [[ -n "$ZSH_TMUX_ITERM2" ]] || ZSH_TMUX_ITERM2=false
  28. # The TERM to use for non-256 color terminals.
  29. # Tmux states this should be screen, but you may need to change it on
  30. # systems without the proper terminfo
  31. [[ -n "$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITHOUT_256COLOR="screen"
  32. # The TERM to use for 256 color terminals.
  33. # Tmux states this should be screen-256color, but you may need to change it on
  34. # systems without the proper terminfo
  35. [[ -n "$ZSH_TMUX_FIXTERM_WITH_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITH_256COLOR="screen-256color"
  36. # Get the absolute path to the current directory
  37. local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
  38. # Determine if the terminal supports 256 colors
  39. if [[ `tput colors` == "256" ]]
  40. 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. # Set the correct local config file to use.
  46. if [[ "$ZSH_TMUX_ITERM2" == "false" ]] && [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
  47. then
  48. #use this when they have a ~/.tmux.conf
  49. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.extra.conf"
  50. else
  51. #use this when they don't have a ~/.tmux.conf
  52. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.only.conf"
  53. fi
  54. # Wrapper function for tmux.
  55. function _zsh_tmux_plugin_run()
  56. {
  57. # We have other arguments, just run them
  58. if [[ -n "$@" ]]
  59. then
  60. \tmux $@
  61. # Try to connect to an existing session.
  62. elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
  63. then
  64. \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
  65. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  66. # Just run tmux, fixing the TERM variable if requested.
  67. else
  68. \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG`
  69. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && 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 [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
  78. then
  79. # Actually don't autostart if we already did and multiple autostarts are disabled.
  80. if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]
  81. then
  82. export ZSH_TMUX_AUTOSTARTED=true
  83. _zsh_tmux_plugin_run
  84. fi
  85. fi
  86. else
  87. print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin."
  88. fi