tmux.plugin.zsh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # The TERM to use for non-256 color terminals.
  18. # Tmux states this should be screen, but you may need to change it on
  19. # systems without the proper terminfo
  20. [[ -n "$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITHOUT_256COLOR="screen"
  21. # The TERM to use for 256 color terminals.
  22. # Tmux states this should be screen-256color, but you may need to change it on
  23. # systems without the proper terminfo
  24. [[ -n "$ZSH_TMUX_FIXTERM_WITH_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITH_256COLOR="screen-256color"
  25. # Get the absolute path to the current directory
  26. local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
  27. # Determine if the terminal supports 256 colors
  28. if [[ `tput colors` == "256" ]]
  29. then
  30. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
  31. else
  32. export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
  33. fi
  34. # Set the correct local config file to use.
  35. if [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
  36. then
  37. #use this when they have a ~/.tmux.conf
  38. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.extra.conf"
  39. else
  40. #use this when they don't have a ~/.tmux.conf
  41. export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.only.conf"
  42. fi
  43. # Wrapper function for tmux.
  44. function _zsh_tmux_plugin_run()
  45. {
  46. # We have other arguments, just run them
  47. if [[ -n "$@" ]]
  48. then
  49. \tmux $@
  50. # Try to connect to an existing session.
  51. elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
  52. then
  53. \tmux attach || \tmux `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG` new-session
  54. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  55. # Just run tmux, fixing the TERM variable if requested.
  56. else
  57. \tmux `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG`
  58. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  59. fi
  60. }
  61. # Use the completions for tmux for our function
  62. compdef _tmux _zsh_tmux_plugin_run
  63. # Alias tmux to our wrapper function.
  64. alias tmux=_zsh_tmux_plugin_run
  65. # Autostart if not already in tmux and enabled.
  66. if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
  67. then
  68. # Actually don't autostart if we already did and multiple autostarts are disabled.
  69. if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]
  70. then
  71. export ZSH_TMUX_AUTOSTARTED=true
  72. _zsh_tmux_plugin_run
  73. fi
  74. fi
  75. else
  76. print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin."
  77. fi