Skip to content

Instantly share code, notes, and snippets.

@jamesharr
Last active August 26, 2020 17:04
Show Gist options
  • Save jamesharr/1179ebaca7009949da5a2b3404aa0aa1 to your computer and use it in GitHub Desktop.
Save jamesharr/1179ebaca7009949da5a2b3404aa0aa1 to your computer and use it in GitHub Desktop.
TMUX snippet
_start_tmux() {
# Skip if we're not interactive
[ -z "$PS1" ] || return
echo $- | grep -qs i || return
# Skip if we're logged in via VSCode
[ -z "$VSCODE_IPC_HOOK_CLI" ] || return
# Skip if we're not on an ssh connection
[ -z "$SSH_CONNECTION" ] && return
# Skip if no tmux installed
hash tmux tmux 2>/dev/null || return
# Skip if we're already in a tmux/screen session
[ "$TERM" = "screen" ] && return
[ "$TERM" = "screen-bce" ] && return
[ -z "$TMUX" ] || return
# Skip on ssh to local host
[[ $SSH_CONNECTION =~ ::1\ .*\ ::1\ .* ]] && return
# Skip if not a hooman on the other side
# This might be covered by _interactive above...
[ -z "$PS1" ] || return
# Start, or attach to a tmux session
t0=$(date +%s)
tmux attach
if [ $? != 0 ]; then
echo "new-session" 1>&2
tmux
fi
t1=$(date +%s)
# Above could be accomplished with this on a newer tmux
# exec tmux new-session -s ssh -A
# Don't exit if we returned fast
if [ $(($t0 + 5)) -gt $t1 ]; then
echo "tmux exited quickly, here's a shell." 1>&2
else
exit
fi
}
_start_tmux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment