zsh: add multiplexer.zsh
Automatically reattach to existing terminal multiplexer session when starting zsh and also create new terminal multiplexer session when connected remotely using ssh.
This commit is contained in:
parent
80a1357651
commit
c6b4c3f7b7
1 changed files with 41 additions and 0 deletions
41
zsh/multiplexer.zsh
Normal file
41
zsh/multiplexer.zsh
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue