tmux.plugin.zsh 3.5 KB

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