Skip to content

Instantly share code, notes, and snippets.

@javipolo
Forked from florianbeer/set-tmux-title
Last active November 29, 2023 09:31
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save javipolo/62eb953f817a9a2f63b8127ff5f60788 to your computer and use it in GitHub Desktop.
Save javipolo/62eb953f817a9a2f63b8127ff5f60788 to your computer and use it in GitHub Desktop.
Set tmux pane title to short hostname on ssh connections
# 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 "$@"
}
@offby1
Copy link

offby1 commented Jul 3, 2018

I found that my tmux program was named tmux-2.1, not tmux, so I generalized things a bit by using case instead of if:

__tm_command() {
    case "$(ps -p $(ps -p $$ -o ppid=) -o comm=| cut -d : -f 1)" in
        tmux*)
            __tm_window=$(__tm_get_current_window)
            trap "tmux set-window-option -t $__tm_window automatic-rename on 1>/dev/null" RETURN
            tmux rename-window "$(__tm_get_hostname $*)"
            ;;
    esac
    command "$@"
}

@nippyin
Copy link

nippyin commented Jun 13, 2021

Hi Mate, Silly question. Where to put code to make it work? I have ~/.tmux.conf where have set few key bindings.

@crpb
Copy link

crpb commented Oct 2, 2021

Hi Mate, Silly question. Where to put code to make it work? I have ~/.tmux.conf where have set few key bindings.

The Code listed are functions. So i would suggest you create an general ~/.functions to put them in and add source ~/.functions to your Shell rcfile like ~/.bashrc or ~/.zshrc.

@0xMH
Copy link

0xMH commented Aug 10, 2022

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 ubuntu@192.168.1.1) 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.

@ScaredyCat
Copy link

ScaredyCat commented Dec 15, 2022

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 ubuntu@192.168.1.1) 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