-
-
Save javipolo/62eb953f817a9a2f63b8127ff5f60788 to your computer and use it in GitHub Desktop.
Set tmux pane title to short hostname on ssh connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make short hostname only if its not an IP address | |
__tm_get_hostname(){ | |
local HOST="$(echo $* | rev | cut -d ' ' -f 1 | rev)" | |
if echo $HOST | grep -P "^([0-9]+\.){3}[0-9]+" -q; then | |
echo $HOST | |
else | |
echo $HOST| cut -d . -f 1 | |
fi | |
} | |
__tm_get_current_window(){ | |
tmux list-windows| awk -F : '/\(active\)$/{print $1}' | |
} | |
# Rename window according to __tm_get_hostname and then restore it after the command | |
__tm_command() { | |
if [ "$(ps -p $(ps -p $$ -o ppid=) -o comm=| cut -d : -f 1)" = "tmux" ]; then | |
__tm_window=$(__tm_get_current_window) | |
# Use current window to change back the setting. If not it will be applied to the active window | |
trap "tmux set-window-option -t $__tm_window automatic-rename on 1>/dev/null" RETURN | |
tmux rename-window "$(__tm_get_hostname $*)" | |
fi | |
command "$@" | |
} | |
ssh() { | |
__tm_command ssh "$@" | |
} | |
ec2ssh() { | |
__tm_command ec2ssh "$@" | |
} |
In the
grep
command on line 4, I think it's better to remove the^
char, Because most of the time the ssh command starts with a user name not an IP i.e (ssh [email protected]) so this condition will mostly be false.so -- if echo $HOST | grep -P "^([0-9]+.){3}[0-9]+" -q; then ++ if echo $HOST | grep -P "([0-9]+.){3}[0-9]+" -q; then Is better on my opinion.
It depends if you've setup your ssh_config with automatically switching users for hosts. It's better to cover as many possibilities as you can.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the
grep
command on line 4, I think it's better to remove the^
char, Because most of the time the ssh command starts with a user name not an IP i.e (ssh [email protected]) so this condition will mostly be false.so
-- if echo $HOST | grep -P "^([0-9]+.){3}[0-9]+" -q; then
++ if echo $HOST | grep -P "([0-9]+.){3}[0-9]+" -q; then
Is better on my opinion.