2018-08-04 04:06:26 +08:00
|
|
|
if ! (( $+commands[tmux] )); then
|
|
|
|
print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ALIASES
|
2013-10-24 14:17:43 +08:00
|
|
|
|
|
|
|
alias ta='tmux attach -t'
|
2016-08-13 11:50:09 +08:00
|
|
|
alias tad='tmux attach -d -t'
|
2013-10-24 14:17:43 +08:00
|
|
|
alias ts='tmux new-session -s'
|
|
|
|
alias tl='tmux list-sessions'
|
2014-09-16 16:35:43 +08:00
|
|
|
alias tksv='tmux kill-server'
|
|
|
|
alias tkss='tmux kill-session -t'
|
2013-10-24 14:17:43 +08:00
|
|
|
|
2018-08-04 04:06:26 +08:00
|
|
|
# CONFIGURATION VARIABLES
|
2015-09-28 10:06:58 +08:00
|
|
|
# Automatically start tmux
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_AUTOSTART:=false}
|
2015-09-28 10:06:58 +08:00
|
|
|
# Only autostart once. If set to false, tmux will attempt to
|
|
|
|
# autostart every time your zsh configs are reloaded.
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_AUTOSTART_ONCE:=true}
|
2015-09-28 10:06:58 +08:00
|
|
|
# Automatically connect to a previous session if it exists
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_AUTOCONNECT:=true}
|
2015-09-28 10:06:58 +08:00
|
|
|
# Automatically close the terminal when tmux exits
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
|
2015-09-28 10:06:58 +08:00
|
|
|
# Set term to screen or screen-256color based on current terminal support
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_FIXTERM:=true}
|
2015-09-28 10:06:58 +08:00
|
|
|
# Set '-CC' option for iTerm2 tmux integration
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_ITERM2:=false}
|
2015-09-28 10:06:58 +08:00
|
|
|
# The TERM to use for non-256 color terminals.
|
|
|
|
# Tmux states this should be screen, but you may need to change it on
|
|
|
|
# systems without the proper terminfo
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
|
2015-09-28 10:06:58 +08:00
|
|
|
# The TERM to use for 256 color terminals.
|
|
|
|
# Tmux states this should be screen-256color, but you may need to change it on
|
|
|
|
# systems without the proper terminfo
|
2018-08-04 04:06:26 +08:00
|
|
|
: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
|
2013-02-27 06:50:15 +08:00
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Determine if the terminal supports 256 colors
|
2018-08-04 04:06:26 +08:00
|
|
|
if [[ $(tput colors) == 256 ]]; then
|
2015-09-28 10:06:58 +08:00
|
|
|
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
|
|
|
|
else
|
|
|
|
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
|
|
|
|
fi
|
2013-02-27 06:50:15 +08:00
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Set the correct local config file to use.
|
2018-08-04 04:06:26 +08:00
|
|
|
if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$HOME/.tmux.conf" ]]; then
|
2015-09-28 10:22:50 +08:00
|
|
|
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
|
2015-09-28 10:06:58 +08:00
|
|
|
else
|
2015-09-28 10:22:50 +08:00
|
|
|
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
|
2015-09-28 10:06:58 +08:00
|
|
|
fi
|
2013-02-27 06:50:15 +08:00
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Wrapper function for tmux.
|
|
|
|
function _zsh_tmux_plugin_run() {
|
|
|
|
if [[ -n "$@" ]]; then
|
2018-08-04 04:06:26 +08:00
|
|
|
command tmux "$@"
|
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
|
|
|
|
local -a tmux_cmd=(command tmux)
|
|
|
|
[[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
|
|
|
|
|
|
|
|
# Try to connect to an existing session.
|
2018-08-07 06:40:18 +08:00
|
|
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
|
2018-08-04 04:06:26 +08:00
|
|
|
|
|
|
|
# If failed, just run tmux, fixing the TERM variable if requested.
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
|
|
|
|
$tmux_cmd new-session
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
|
|
|
|
exit
|
2015-09-28 10:06:58 +08:00
|
|
|
fi
|
|
|
|
}
|
2013-02-27 23:21:19 +08:00
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Use the completions for tmux for our function
|
|
|
|
compdef _tmux _zsh_tmux_plugin_run
|
|
|
|
# Alias tmux to our wrapper function.
|
|
|
|
alias tmux=_zsh_tmux_plugin_run
|
2013-02-27 07:09:01 +08:00
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Autostart if not already in tmux and enabled.
|
2018-08-04 04:06:26 +08:00
|
|
|
if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]; then
|
2015-09-28 10:06:58 +08:00
|
|
|
# Actually don't autostart if we already did and multiple autostarts are disabled.
|
|
|
|
if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
|
|
|
|
export ZSH_TMUX_AUTOSTARTED=true
|
|
|
|
_zsh_tmux_plugin_run
|
|
|
|
fi
|
2013-02-27 07:09:01 +08:00
|
|
|
fi
|