Skip to content

Instantly share code, notes, and snippets.

@nevesnunes
Forked from florianbeer/set-tmux-title
Created July 6, 2020 12:55
Show Gist options
  • Save nevesnunes/951f77085116a9beea62c9610dda31e8 to your computer and use it in GitHub Desktop.
Save nevesnunes/951f77085116a9beea62c9610dda31e8 to your computer and use it in GitHub Desktop.
Set tmux pane title to short hostname on ssh connections
ssh() {
# grep -w: match command names such as "tmux-2.1" or "tmux: server"
if ps -p $$ -o ppid= \
| xargs -i ps -p {} -o comm= \
| grep -qw tmux; then
# Note: Options without parameter were hardcoded,
# in order to distinguish an option's parameter from the destination.
#
# s/[[:space:]]*\(\( | spaces before options
# \(-[46AaCfGgKkMNnqsTtVvXxYy]\)\| | option without parameter
# \(-[^[:space:]]* | option
# \([[:space:]]\+[^[:space:]]*\)\?\)\) | parameter
# [[:space:]]*\)* | spaces between options
# [[:space:]]\+ | spaces before destination
# \([^-][^[:space:]]*\) | destination
# .* | command
# /\6/ | replace with destination
tmux rename-window "$(echo "$@" \
| sed 's/[[:space:]]*\(\(\(-[46AaCfGgKkMNnqsTtVvXxYy]\)\|\(-[^[:space:]]*\([[:space:]]\+[^[:space:]]*\)\?\)\)[[:space:]]*\)*[[:space:]]\+\([^-][^[:space:]]*\).*/\6/')"
command ssh "$@"
tmux set-window-option automatic-rename "on" 1> /dev/null
else
command ssh "$@"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment