tmux.plugin.zsh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Only run if tmux is actually installed
  2. if which tmux &> /dev/null
  3. then
  4. # Configuration variables
  5. #
  6. # Automatically start tmux
  7. [[ -n "$ZSH_TMUX_AUTOSTART" ]] || 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. [[ -n "$ZSH_TMUX_AUTOSTART_ONCE" ]] || ZSH_TMUX_AUTOSTART_ONCE=true
  11. # Automatically connect to a previous session if it exists
  12. [[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true
  13. # Automatically close the terminal when tmux exits
  14. [[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART
  15. # Set term to screen or screen-256color based on current terminal support
  16. [[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true
  17. # Set '-CC' option for iTerm2 tmux integration
  18. [[ -n "$ZSH_TMUX_ITERM2" ]] || 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. [[ -n "$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR" ]] || 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. [[ -n "$ZSH_TMUX_FIXTERM_WITH_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITH_256COLOR="screen-256color"
  27. # Get the absolute path to the current directory
  28. local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
  29. # Determine if the terminal supports 256 colors
  30. if [[ `tput colors` == "256" ]]
  31. then
  32. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
  33. else
  34. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
  35. fi
  36. # Set the correct local config file to use.
  37. if [[ "$ZSH_TMUX_ITERM2" == "false" ]] && (( [[ -f $HOME/.tmux.conf ]] || -h $HOME/.tmux.conf ]] ))
  38. then
  39. #use this when they have a ~/.tmux.conf
  40. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.extra.conf"
  41. else
  42. #use this when they don't have a ~/.tmux.conf
  43. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.only.conf"
  44. fi
  45. # Wrapper function for tmux.
  46. function _zsh_tmux_plugin_run()
  47. {
  48. # We have other arguments, just run them
  49. if [[ -n "$@" ]]
  50. then
  51. \tmux $@
  52. # Try to connect to an existing session.
  53. elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
  54. then
  55. \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
  56. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  57. # Just run tmux, fixing the TERM variable if requested.
  58. else
  59. \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG`
  60. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  61. fi
  62. }
  63. # Use the completions for tmux for our function
  64. compdef _tmux _zsh_tmux_plugin_run
  65. # Alias tmux to our wrapper function.
  66. alias tmux=_zsh_tmux_plugin_run
  67. # Autostart if not already in tmux and enabled.
  68. if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
  69. then
  70. # Actually don't autostart if we already did and multiple autostarts are disabled.
  71. if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]
  72. then
  73. export ZSH_TMUX_AUTOSTARTED=true
  74. _zsh_tmux_plugin_run
  75. fi
  76. fi
  77. else
  78. print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin."
  79. fi