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
|
|
|
|
|
|
|
|
# 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}
|
2024-04-16 15:52:12 +08:00
|
|
|
# Automatically name the new session based on the basename of PWD
|
|
|
|
: ${ZSH_TMUX_AUTONAME_SESSION:=false}
|
2020-02-05 02:41:39 +08:00
|
|
|
# Set term to screen or screen-256color based on current terminal support
|
2024-02-28 03:09:02 +08:00
|
|
|
: ${ZSH_TMUX_DETACHED:=false}
|
|
|
|
# Set detached mode
|
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.
|
2024-02-28 17:52:09 +08:00
|
|
|
# Tmux states this should be tmux|screen, but you may need to change it on
|
2015-09-28 10:06:58 +08:00
|
|
|
# systems without the proper terminfo
|
2024-02-28 17:52:09 +08:00
|
|
|
if [[ -e /usr/share/terminfo/t/tmux ]]; then
|
|
|
|
: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=tmux}
|
|
|
|
else
|
|
|
|
: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
|
|
|
|
fi
|
2015-09-28 10:06:58 +08:00
|
|
|
# The TERM to use for 256 color terminals.
|
2024-02-28 17:52:09 +08:00
|
|
|
# Tmux states this should be (tmux|screen)-256color, but you may need to change it on
|
2015-09-28 10:06:58 +08:00
|
|
|
# systems without the proper terminfo
|
2024-02-28 17:52:09 +08:00
|
|
|
if [[ -e /usr/share/terminfo/t/tmux-256color ]]; then
|
|
|
|
: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=tmux-256color}
|
|
|
|
else
|
|
|
|
: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
|
|
|
|
fi
|
2019-11-09 04:44:44 +08:00
|
|
|
# Set the configuration path
|
2023-09-03 23:38:32 +08:00
|
|
|
if [[ -e $HOME/.tmux.conf ]]; then
|
|
|
|
: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
|
|
|
|
elif [[ -e ${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf ]]; then
|
|
|
|
: ${ZSH_TMUX_CONFIG:=${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf}
|
|
|
|
else
|
|
|
|
: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
|
|
|
|
fi
|
2016-06-16 09:05:09 +08:00
|
|
|
# Set -u option to support unicode
|
|
|
|
: ${ZSH_TMUX_UNICODE:=false}
|
2013-02-27 06:50:15 +08:00
|
|
|
|
2022-01-28 00:59:04 +08:00
|
|
|
# ALIASES
|
2024-03-01 02:05:54 +08:00
|
|
|
function _build_tmux_alias {
|
2024-07-16 00:45:46 +08:00
|
|
|
setopt localoptions no_rc_expand_param
|
2024-03-01 02:05:54 +08:00
|
|
|
eval "function $1 {
|
2024-03-02 16:12:34 +08:00
|
|
|
if [[ -z \$1 ]] || [[ \${1:0:1} == '-' ]]; then
|
2024-03-01 02:05:54 +08:00
|
|
|
tmux $2 \"\$@\"
|
|
|
|
else
|
|
|
|
tmux $2 $3 \"\$@\"
|
|
|
|
fi
|
|
|
|
}"
|
2024-06-03 18:53:27 +08:00
|
|
|
|
|
|
|
local f s
|
|
|
|
f="_omz_tmux_alias_${1}"
|
|
|
|
s=(${(z)2})
|
|
|
|
|
|
|
|
eval "function ${f}() {
|
|
|
|
shift words;
|
|
|
|
words=(tmux ${@:2} \$words);
|
|
|
|
((CURRENT+=${#s[@]}+1))
|
|
|
|
_tmux
|
|
|
|
}"
|
|
|
|
|
|
|
|
compdef "$f" "$1"
|
2024-03-01 02:05:54 +08:00
|
|
|
}
|
|
|
|
|
2022-01-28 00:59:04 +08:00
|
|
|
alias tksv='tmux kill-server'
|
2024-03-01 02:05:54 +08:00
|
|
|
alias tl='tmux list-sessions'
|
2022-01-28 00:59:04 +08:00
|
|
|
alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
|
|
|
|
|
2024-03-01 02:05:54 +08:00
|
|
|
_build_tmux_alias "ta" "attach" "-t"
|
|
|
|
_build_tmux_alias "tad" "attach -d" "-t"
|
|
|
|
_build_tmux_alias "ts" "new-session" "-s"
|
|
|
|
_build_tmux_alias "tkss" "kill-session" "-t"
|
|
|
|
|
|
|
|
unfunction _build_tmux_alias
|
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Determine if the terminal supports 256 colors
|
2018-11-27 02:57:52 +08:00
|
|
|
if [[ $terminfo[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
|
|
|
|
2021-12-19 04:53:49 +08:00
|
|
|
# Handle $0 according to the standard:
|
2021-12-28 21:16:54 +08:00
|
|
|
# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
|
2021-12-19 04:53:49 +08:00
|
|
|
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
|
|
|
|
0="${${(M)0:#/*}:-$PWD/$0}"
|
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Set the correct local config file to use.
|
2019-11-09 04:44:44 +08:00
|
|
|
if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
|
2019-11-15 23:47:11 +08:00
|
|
|
export ZSH_TMUX_CONFIG
|
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
|
|
|
|
|
2018-08-09 23:19:40 +08:00
|
|
|
local -a tmux_cmd
|
|
|
|
tmux_cmd=(command tmux)
|
2018-08-04 04:06:26 +08:00
|
|
|
[[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
|
2016-06-16 09:05:09 +08:00
|
|
|
[[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
|
2018-08-04 04:06:26 +08:00
|
|
|
|
2024-03-01 23:44:20 +08:00
|
|
|
local _detached=""
|
|
|
|
[[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d"
|
2024-04-16 15:52:12 +08:00
|
|
|
|
|
|
|
local session_name
|
|
|
|
if [[ "$ZSH_TMUX_AUTONAME_SESSION" == "true" ]]; then
|
|
|
|
# Name the session after the basename of the current directory
|
|
|
|
session_name=${PWD##*/}
|
|
|
|
# If the current directory is the home directory, name it 'HOME'
|
|
|
|
[[ "$PWD" == "$HOME" ]] && session_name="HOME"
|
|
|
|
# If the current directory is the root directory, name it 'ROOT'
|
|
|
|
[[ "$PWD" == "/" ]] && session_name="ROOT"
|
|
|
|
else
|
|
|
|
session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
|
|
|
|
fi
|
|
|
|
|
2018-08-04 04:06:26 +08:00
|
|
|
# Try to connect to an existing session.
|
2024-04-16 15:52:12 +08:00
|
|
|
if [[ -n "$session_name" ]]; then
|
|
|
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
|
2022-12-08 19:38:13 +08:00
|
|
|
else
|
2024-03-01 23:44:20 +08:00
|
|
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached
|
2022-12-08 19:38:13 +08:00
|
|
|
fi
|
2018-08-04 04:06:26 +08:00
|
|
|
|
|
|
|
# If failed, just run tmux, fixing the TERM variable if requested.
|
|
|
|
if [[ $? -ne 0 ]]; then
|
2019-11-14 20:34:02 +08: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
|
2024-04-16 15:52:12 +08:00
|
|
|
|
|
|
|
if [[ -n "$session_name" ]]; then
|
|
|
|
$tmux_cmd new-session -s "$session_name"
|
2021-11-08 22:32:09 +08:00
|
|
|
else
|
2022-12-08 19:38:13 +08:00
|
|
|
$tmux_cmd new-session
|
2021-11-08 22:32:09 +08:00
|
|
|
fi
|
2018-08-04 04:06:26 +08:00
|
|
|
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
|
|
|
|
2023-10-19 17:49:38 +08:00
|
|
|
function _tmux_directory_session() {
|
|
|
|
# current directory without leading path
|
|
|
|
local dir=${PWD##*/}
|
|
|
|
# md5 hash for the full working directory path
|
|
|
|
local md5=$(printf '%s' "$PWD" | md5sum | cut -d ' ' -f 1)
|
|
|
|
# human friendly unique session name for this directory
|
|
|
|
local session_name="${dir}-${md5:0:6}"
|
|
|
|
# create or attach to the session
|
|
|
|
tmux new -As "$session_name"
|
|
|
|
}
|
|
|
|
|
|
|
|
alias tds=_tmux_directory_session
|
|
|
|
|
2015-09-28 10:06:58 +08:00
|
|
|
# Autostart if not already in tmux and enabled.
|
2023-10-03 02:34:57 +08:00
|
|
|
if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" ]]; 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
|