Automatically reattach to existing terminal multiplexer session when starting zsh and also create new terminal multiplexer session when connected remotely using ssh.
41 lines
1.4 KiB
Bash
41 lines
1.4 KiB
Bash
zshrc_use_multiplexer=tmux
|
|
|
|
# Make sure the set multiplexer is available. Otherwise using exec will
|
|
# terminate our session.
|
|
if [[ -n $zshrc_use_multiplexer ]]; then
|
|
if ! (( $+commands[$zshrc_use_multiplexer] )); then
|
|
echo "Multiplexer '$zshrc_use_multiplexer' not found." >&2
|
|
zshrc_use_multiplexer=
|
|
fi
|
|
fi
|
|
|
|
# If not already in screen or tmux, reattach to a running session or create a
|
|
# new one when connecting through ssh.
|
|
if [[ $TERM != dumb && $TERM != dialup && $TERM != linux
|
|
&& -z $STY && -z $TMUX ]]; then
|
|
# Get running detached sessions.
|
|
if [[ $zshrc_use_multiplexer = screen ]]; then
|
|
session=$(screen -list | grep 'Detached' | awk '{ print $1; exit }')
|
|
elif [[ $zshrc_use_multiplexer = tmux ]]; then
|
|
session=$(tmux list-sessions 2>/dev/null \
|
|
| sed '/(attached)$/ d; s/^\([0-9]\{1,\}\).*$/\1/; q')
|
|
fi
|
|
|
|
# Create a new remote session if none is running.
|
|
if [[ -n $SSH_CONNECTION && -z $session ]]; then
|
|
if [[ $zshrc_use_multiplexer = screen ]]; then
|
|
exec screen
|
|
elif [[ $zshrc_use_multiplexer = tmux ]]; then
|
|
exec tmux
|
|
fi
|
|
fi
|
|
|
|
# Reattach to a running session.
|
|
if [[ -v $session ]]; then
|
|
if [[ $zshrc_use_multiplexer = screen ]]; then
|
|
exec screen -r $session
|
|
elif [[ $zshrc_use_multiplexer = tmux ]]; then
|
|
exec tmux attach-session -t $session
|
|
fi
|
|
fi
|
|
fi
|