1 if ! (( $+commands[tmux] )); then
2 print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
6 # CONFIGURATION VARIABLES
7 # Automatically start tmux
8 : ${ZSH_TMUX_AUTOSTART:=false}
9 # Only autostart once. If set to false, tmux will attempt to
10 # autostart every time your zsh configs are reloaded.
11 : ${ZSH_TMUX_AUTOSTART_ONCE:=true}
12 # Automatically connect to a previous session if it exists
13 : ${ZSH_TMUX_AUTOCONNECT:=true}
14 # Automatically close the terminal when tmux exits
15 : ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
16 # Set term to screen or screen-256color based on current terminal support
17 : ${ZSH_TMUX_FIXTERM:=true}
18 # Set '-CC' option for iTerm2 tmux integration
19 : ${ZSH_TMUX_ITERM2:=false}
20 # The TERM to use for non-256 color terminals.
21 # Tmux states this should be screen, but you may need to change it on
22 # systems without the proper terminfo
23 : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
24 # The TERM to use for 256 color terminals.
25 # Tmux states this should be screen-256color, but you may need to change it on
26 # systems without the proper terminfo
27 : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
28 # Set the configuration path
29 : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
30 # Set -u option to support unicode
31 : ${ZSH_TMUX_UNICODE:=false}
35 alias ta='tmux attach -t'
36 alias tad='tmux attach -d -t'
37 alias ts='tmux new-session -s'
38 alias tl='tmux list-sessions'
39 alias tksv='tmux kill-server'
40 alias tkss='tmux kill-session -t'
41 alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
43 # Determine if the terminal supports 256 colors
44 if [[ $terminfo[colors] == 256 ]]; then
45 export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
47 export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
50 # Handle $0 according to the standard:
51 # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
52 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
53 0="${${(M)0:#/*}:-$PWD/$0}"
55 # Set the correct local config file to use.
56 if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
57 export ZSH_TMUX_CONFIG
58 export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
60 export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
63 # Wrapper function for tmux.
64 function _zsh_tmux_plugin_run() {
65 if [[ -n "$@" ]]; then
71 tmux_cmd=(command tmux)
72 [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
73 [[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
75 # Try to connect to an existing session.
76 [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
78 # If failed, just run tmux, fixing the TERM variable if requested.
79 if [[ $? -ne 0 ]]; then
80 if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
81 tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
82 elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
83 tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
85 if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
86 $tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_SESSION_NAME
92 if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
97 # Use the completions for tmux for our function
98 compdef _tmux _zsh_tmux_plugin_run
99 # Alias tmux to our wrapper function.
100 alias tmux=_zsh_tmux_plugin_run
102 # Autostart if not already in tmux and enabled.
103 if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
104 # Actually don't autostart if we already did and multiple autostarts are disabled.
105 if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
106 export ZSH_TMUX_AUTOSTARTED=true