tmux.plugin.zsh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Configuration variables
  2. #
  3. # Automatically start tmux
  4. [[ -n "$ZSH_TMUX_AUTOSTART" ]] || ZSH_TMUX_AUTOSTART=false
  5. # Only autostart once. If set to false, tmux will attempt to
  6. # autostart every time your zsh configs are reloaded.
  7. [[ -n "$ZSH_TMUX_AUTOSTART_ONCE" ]] || ZSH_TMUX_AUTOSTART_ONCE=true
  8. # Automatically connect to a previous session if it exists
  9. [[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true
  10. # Automatically close the terminal when tmux exits
  11. [[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART
  12. # Set term to screen or screen-256color based on current terminal support
  13. [[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true
  14. # Get the absolute path to the current directory
  15. local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
  16. # Determine if the terminal supports 256 colors
  17. if [[ `tput colors` == "256" ]]
  18. then
  19. export ZSH_TMUX_TERM="screen-256color"
  20. else
  21. export ZSH_TMUX_TERM="screen"
  22. fi
  23. # Local variable to store the local config file to use, if any.
  24. local fixed_config=""
  25. # Set the correct local config file to use.
  26. if [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
  27. then
  28. #use this when they have a ~/.tmux.conf
  29. fixed_config="$zsh_tmux_plugin_path/tmux.extra.conf"
  30. else
  31. #use this when they don't have a ~/.tmux.conf
  32. fixed_config="$zsh_tmux_plugin_path/tmux.only.conf"
  33. fi
  34. # Wrapper function for tmux.
  35. function zsh_tmux_plugin_run()
  36. {
  37. # We have other arguments, just run them
  38. if [[ -n "$@" ]]
  39. then
  40. \tmux $@
  41. # Try to connect to an existing session.
  42. elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
  43. then
  44. \tmux attach || \tmux `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$fixed_config` new-session
  45. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  46. # Just run tmux, fixing the TERM variable if requested.
  47. else
  48. \tmux `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$fixed_config`
  49. [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
  50. fi
  51. }
  52. # Use the completions for tmux for our function
  53. compdef _tmux zsh_tmux_plugin_run
  54. # Alias tmux to our wrapper function.
  55. alias tmux=zsh_tmux_plugin_run
  56. # Autostart if not already in tmux and enabled.
  57. if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
  58. then
  59. # Actually don't autostart if we already did and multiple autostarts are disabled.
  60. if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]
  61. then
  62. export ZSH_TMUX_AUTOSTARTED=true
  63. zsh_tmux_plugin_run
  64. fi
  65. fi