Skip to content

Instantly share code, notes, and snippets.

@florianbeer
Created April 21, 2015 10:47
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save florianbeer/ee02c149a7e25f643491 to your computer and use it in GitHub Desktop.
Save florianbeer/ee02c149a7e25f643491 to your computer and use it in GitHub Desktop.
Set tmux pane title to short hostname on ssh connections
ssh() {
if [ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" = "tmux" ]; then
tmux rename-window "$(echo $* | cut -d . -f 1)"
command ssh "$@"
tmux set-window-option automatic-rename "on" 1>/dev/null
else
command ssh "$@"
fi
}
@maptracker
Copy link

Thanks for this! It is unhappy with flags on ssh (like -X) though. I used another trick to get just the last argument:

tmux rename-window "$(echo $* | rev | cut -d ' ' -f1 | rev | cut -d . -f 1)"

@javipolo
Copy link

Thanks a lot, I've been using this for quite a long time, and I decided to improve it by:

  • Do not use short name if it's an IP address
  • Putting everything into functions that can be reused by any other command
  • Determine the proper window ID to restore after the command. If not, in case the ssh failed while you were working on other window, it would restore the automatic-rename setting in the active window instead of the proper one

https://gist.github.com/javipolo/62eb953f817a9a2f63b8127ff5f60788

@arno01
Copy link

arno01 commented Oct 17, 2018

Working variant in Termux on Samsung DeX:

# Requires procps package installed.
ssh() {
  if [ "$(ps -p $(ps -p $$ -o ppid=) -o comm= | cut -d : -f1)" = "tmux" ]; then
    tmux rename-window "$(echo -- $* | awk '{print $NF}')"
    command ssh "$@"
    tmux set-window-option automatic-rename "on" 1>/dev/null
  else
    command ssh "$@"
  fi
}

@int32bit
Copy link

int32bit commented Jul 8, 2019

It will go wrong if I run with '-i' option like ssh -i my.pem test@x.x.x.x .

@nevesnunes
Copy link

nevesnunes commented Jul 3, 2020

As @int32bit mentioned, options aren't handled in the previous snippets. The following variant handles those cases:

https://gist.github.com/nevesnunes/951f77085116a9beea62c9610dda31e8

@nippyin
Copy link

nippyin commented Jun 13, 2021

Could you please share where would I place this code ? I pasted this in my ~/.zshrc file and ssh to a server but I didn't see remote host name anywhere.

Note: I do have ~/.tmux.config file which have few keybindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment