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
|
|
|
|
|
|
|
|
# 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}
|
2024-04-16 09:52:12 +02:00
|
|
|
# Automatically name the new session based on the basename of PWD
|
|
|
|
: ${ZSH_TMUX_AUTONAME_SESSION:=false}
|
2020-02-04 19:41:39 +01:00
|
|
|
# Set term to screen or screen-256color based on current terminal support
|
2024-02-27 20:09:02 +01:00
|
|
|
: ${ZSH_TMUX_DETACHED:=false}
|
|
|
|
# Set detached mode
|
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.
|
2024-02-28 10:52:09 +01:00
|
|
|
# Tmux states this should be tmux|screen, but you may need to change it on
|
2015-09-28 04:06:58 +02:00
|
|
|
# systems without the proper terminfo
|
2024-02-28 10:52:09 +01: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 04:06:58 +02:00
|
|
|
# The TERM to use for 256 color terminals.
|
2024-02-28 10:52:09 +01:00
|
|
|
# Tmux states this should be (tmux|screen)-256color, but you may need to change it on
|
2015-09-28 04:06:58 +02:00
|
|
|
# systems without the proper terminfo
|
2024-02-28 10:52:09 +01: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-08 21:44:44 +01:00
|
|
|
# Set the configuration path
|
2023-09-03 17:38:32 +02: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 03:05:09 +02:00
|
|
|
# Set -u option to support unicode
|
|
|
|
: ${ZSH_TMUX_UNICODE:=false}
|
2013-02-26 23:50:15 +01:00
|
|
|
|
2022-01-27 17:59:04 +01:00
|
|
|
# ALIASES
|
2024-02-29 19:05:54 +01:00
|
|
|
function _build_tmux_alias {
|
2024-07-15 18:45:46 +02:00
|
|
|
setopt localoptions no_rc_expand_param
|
2024-02-29 19:05:54 +01:00
|
|
|
eval "function $1 {
|
2024-03-02 09:12:34 +01:00
|
|
|
if [[ -z \$1 ]] || [[ \${1:0:1} == '-' ]]; then
|
2024-02-29 19:05:54 +01:00
|
|
|
tmux $2 \"\$@\"
|
|
|
|
else
|
|
|
|
tmux $2 $3 \"\$@\"
|
|
|
|
fi
|
|
|
|
}"
|
2024-06-03 12:53:27 +02: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-02-29 19:05:54 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 17:59:04 +01:00
|
|
|
alias tksv='tmux kill-server'
|
2024-02-29 19:05:54 +01:00
|
|
|
alias tl='tmux list-sessions'
|
2022-01-27 17:59:04 +01:00
|
|
|
alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
|
|
|
|
|
2024-02-29 19:05:54 +01: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 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
|
|
|
|
2021-12-18 21:53:49 +01:00
|
|
|
# Handle $0 according to the standard:
|
2021-12-28 14:16:54 +01:00
|
|
|
# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
|
2021-12-18 21:53:49 +01:00
|
|
|
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
|
|
|
|
0="${${(M)0:#/*}:-$PWD/$0}"
|
|
|
|
|
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
|
2019-11-15 16:47:11 +01:00
|
|
|
export ZSH_TMUX_CONFIG
|
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)
|
2016-06-16 03:05:09 +02:00
|
|
|
[[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
|
2018-08-03 22:06:26 +02:00
|
|
|
|
2024-03-01 16:44:20 +01:00
|
|
|
local _detached=""
|
|
|
|
[[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d"
|
2024-04-16 09:52:12 +02: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-03 22:06:26 +02:00
|
|
|
# Try to connect to an existing session.
|
2024-04-16 09:52:12 +02:00
|
|
|
if [[ -n "$session_name" ]]; then
|
|
|
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
|
2022-12-08 12:38:13 +01:00
|
|
|
else
|
2024-03-01 16:44:20 +01:00
|
|
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached
|
2022-12-08 12:38:13 +01:00
|
|
|
fi
|
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-14 13:34:02 +01: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 09:52:12 +02:00
|
|
|
|
|
|
|
if [[ -n "$session_name" ]]; then
|
|
|
|
$tmux_cmd new-session -s "$session_name"
|
2021-11-08 15:32:09 +01:00
|
|
|
else
|
2022-12-08 12:38:13 +01:00
|
|
|
$tmux_cmd new-session
|
2021-11-08 15:32:09 +01:00
|
|
|
fi
|
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
|
|
|
|
2023-10-19 11:49:38 +02: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 04:06:58 +02:00
|
|
|
# Autostart if not already in tmux and enabled.
|
2023-10-02 20:34:57 +02:00
|
|
|
if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" ]]; 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
|