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}
|
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 {
|
|
|
|
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
|
|
|
|
}"
|
|
|
|
}
|
|
|
|
|
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"
|
2018-08-03 22:06:26 +02:00
|
|
|
# Try to connect to an existing session.
|
2022-12-08 12:38:13 +01:00
|
|
|
if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
|
2024-03-01 16:44:20 +01:00
|
|
|
[[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t $ZSH_TMUX_DEFAULT_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
|
2021-11-08 15:32:09 +01:00
|
|
|
if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
|
2022-12-08 12:38:13 +01:00
|
|
|
$tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_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
|