2018-08-03 22:06:26 +02: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 08:17:43 +02:00
|
|
|
|
|
|
|
alias ta='tmux attach -t'
|
2016-08-13 05:50:09 +02:00
|
|
|
alias tad='tmux attach -d -t'
|
2013-10-24 08:17:43 +02:00
|
|
|
alias ts='tmux new-session -s'
|
|
|
|
alias tl='tmux list-sessions'
|
2014-09-16 10:35:43 +02:00
|
|
|
alias tksv='tmux kill-server'
|
|
|
|
alias tkss='tmux kill-session -t'
|
2013-10-24 08:17:43 +02:00
|
|
|
|
2018-08-03 22:06:26 +02:00
|
|
|
# CONFIGURATION VARIABLES
|
2015-09-28 04:06:58 +02:00
|
|
|
# Automatically start tmux
|
2018-08-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_AUTOSTART:=false}
|
2015-09-28 04:06:58 +02:00
|
|
|
# Only autostart once. If set to false, tmux will attempt to
|
|
|
|
# autostart every time your zsh configs are reloaded.
|
2018-08-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_AUTOSTART_ONCE:=true}
|
2015-09-28 04:06:58 +02:00
|
|
|
# Automatically connect to a previous session if it exists
|
2018-08-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_AUTOCONNECT:=true}
|
2015-09-28 04:06:58 +02:00
|
|
|
# Automatically close the terminal when tmux exits
|
2018-08-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
|
2015-09-28 04:06:58 +02:00
|
|
|
# Set term to screen or screen-256color based on current terminal support
|
2018-08-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_FIXTERM:=true}
|
2015-09-28 04:06:58 +02:00
|
|
|
# Set '-CC' option for iTerm2 tmux integration
|
2018-08-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_ITERM2:=false}
|
2015-09-28 04:06:58 +02: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-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
|
2015-09-28 04:06:58 +02: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-03 22:06:26 +02:00
|
|
|
: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
|
2019-11-08 21:44:44 +01:00
|
|
|
# Set the configuration path
|
|
|
|
: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
|
2013-02-26 23:50:15 +01:00
|
|
|
|
2015-09-28 04:06:58 +02:00
|
|
|
# Determine if the terminal supports 256 colors
|
2018-11-26 19:57:52 +01:00
|
|
|
if [[ $terminfo[colors] == 256 ]]; then
|
2015-09-28 04:06:58 +02:00
|
|
|
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
|
|
|
|
else
|
|
|
|
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
|
|
|
|
fi
|
2013-02-26 23:50:15 +01:00
|
|
|
|
2015-09-28 04:06:58 +02:00
|
|
|
# Set the correct local config file to use.
|
2019-11-08 21:44:44 +01:00
|
|
|
if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
|
2015-09-28 04:22:50 +02:00
|
|
|
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
|
2015-09-28 04:06:58 +02:00
|
|
|
else
|
2015-09-28 04:22:50 +02:00
|
|
|
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
|
2015-09-28 04:06:58 +02:00
|
|
|
fi
|
2013-02-26 23:50:15 +01:00
|
|
|
|
2015-09-28 04:06:58 +02:00
|
|
|
# Wrapper function for tmux.
|
|
|
|
function _zsh_tmux_plugin_run() {
|
|
|
|
if [[ -n "$@" ]]; then
|
2018-08-03 22:06:26 +02:00
|
|
|
command tmux "$@"
|
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
|
2018-08-09 17:19:40 +02:00
|
|
|
local -a tmux_cmd
|
|
|
|
tmux_cmd=(command tmux)
|
2018-08-03 22:06:26 +02:00
|
|
|
[[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
|
|
|
|
|
|
|
|
# Try to connect to an existing session.
|
2018-08-07 00:40:18 +02:00
|
|
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
|
2018-08-03 22:06:26 +02:00
|
|
|
|
|
|
|
# If failed, just run tmux, fixing the TERM variable if requested.
|
|
|
|
if [[ $? -ne 0 ]]; then
|
2019-11-08 21:44:44 +01:00
|
|
|
[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG") || \
|
|
|
|
[[ -e "$ZSH_TMUX_CONFIG" ]] && tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
|
|
|
|
$tmux_cmd new-session
|
2018-08-03 22:06:26 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
|
|
|
|
exit
|
2015-09-28 04:06:58 +02:00
|
|
|
fi
|
|
|
|
}
|
2013-02-27 16:21:19 +01:00
|
|
|
|
2015-09-28 04:06:58 +02: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 00:09:01 +01:00
|
|
|
|
2015-09-28 04:06:58 +02:00
|
|
|
# Autostart if not already in tmux and enabled.
|
2017-04-26 09:56:50 +02:00
|
|
|
if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
|
2015-09-28 04:06:58 +02: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 00:09:01 +01:00
|
|
|
fi
|