conf2/zsh/.oh-my-zsh/plugins/tmux/tmux.plugin.zsh

100 lines
3.2 KiB
Bash
Raw Normal View History

2019-01-12 15:47:23 +00:00
if ! (( $+commands[tmux] )); then
print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
return 1
fi
# ALIASES
alias ta='tmux attach -t'
alias tad='tmux attach -d -t'
alias ts='tmux new-session -s'
alias tl='tmux list-sessions'
alias tksv='tmux kill-server'
alias tkss='tmux kill-session -t'
# CONFIGURATION VARIABLES
# Automatically start tmux
: ${ZSH_TMUX_AUTOSTART:=false}
# Only autostart once. If set to false, tmux will attempt to
# autostart every time your zsh configs are reloaded.
: ${ZSH_TMUX_AUTOSTART_ONCE:=true}
# Automatically connect to a previous session if it exists
: ${ZSH_TMUX_AUTOCONNECT:=true}
# Automatically close the terminal when tmux exits
: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
2020-02-02 15:08:42 +00:00
# Set term to tmux or tmux-256color based on current terminal support
2019-01-12 15:47:23 +00:00
: ${ZSH_TMUX_FIXTERM:=true}
# Set '-CC' option for iTerm2 tmux integration
: ${ZSH_TMUX_ITERM2:=false}
# The TERM to use for non-256 color terminals.
2020-02-02 15:08:42 +00:00
# Tmux states this should be tmux, but you may need to change it on
2019-01-12 15:47:23 +00:00
# systems without the proper terminfo
2020-02-02 15:08:42 +00:00
: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=tmux}
2019-01-12 15:47:23 +00:00
# The TERM to use for 256 color terminals.
2020-02-02 15:08:42 +00:00
# Tmux states this should be tmux-256color, but you may need to change it on
2019-01-12 15:47:23 +00:00
# systems without the proper terminfo
2020-02-02 15:08:42 +00:00
: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=tmux-256color}
2019-11-22 19:01:23 +00:00
# Set the configuration path
: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
2019-12-21 16:33:06 +00:00
# Set -u option to support unicode
: ${ZSH_TMUX_UNICODE:=false}
2019-01-12 15:47:23 +00:00
# Determine if the terminal supports 256 colors
if [[ $terminfo[colors] == 256 ]]; then
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
else
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
fi
# Set the correct local config file to use.
2019-11-22 19:01:23 +00:00
if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
export ZSH_TMUX_CONFIG
2019-01-12 15:47:23 +00:00
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
else
export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
fi
# Wrapper function for tmux.
function _zsh_tmux_plugin_run() {
if [[ -n "$@" ]]; then
command tmux "$@"
return $?
fi
local -a tmux_cmd
tmux_cmd=(command tmux)
[[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
2019-12-21 16:33:06 +00:00
[[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
2019-01-12 15:47:23 +00:00
# Try to connect to an existing session.
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
# If failed, just run tmux, fixing the TERM variable if requested.
if [[ $? -ne 0 ]]; then
2019-11-22 19:01:23 +00:00
if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
fi
2019-01-12 15:47:23 +00:00
$tmux_cmd new-session
fi
if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
exit
fi
}
# 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
# Autostart if not already in tmux and enabled.
if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
# 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
fi