EDIT: On Yosemite (OS X 10.10) use this instead:
function ssh ()
{
servername=$(echo $@ | sed -E 's|([- a-zA-Z0-9:/]+)( *[^@ ]*@)([^@ ]*)|\3|')
echo -ne "\033]0;${servername}\007";
/usr/bin/ssh $@
}
Note that the function expects username and server name to be specified as user@server
(you can use the server IP address too: user@a.b.c.d
, the tab name will be set to a.b.c.d
). Other combinations like -l user server
won't work properly.
You can unset the function with:
unset -f ssh
You can create a function named ssh
that sets the terminal tab name and then invokes /usr/sbin/ssh
:
function ssh ()
{
servername=$(echo $@ | sed 's|\([- a-zA-Z0-9:/]\+\) *\([^-]\+@\)*\([-a-zA-Z0-9.]\+\)|\3|' | awk '{print $1}');
echo -ne "\033]0;${servername}\007";
/usr/bin/ssh $@
}
servername=$(echo $@ ...
The first line extracts the server name from the arguments passed to ssh
. I tested it against several possible ssh
invocations with and without arguments but can't promise it's bullet proof.
echo -ne "\033]0;${servername}\007";
The second line is where the magic happens.
\033]0;<name>\007
is a ESCAPE sequence that sets the terminal tab name. See this Wikipedia article for more information - this particular escape code is listed in the non-CSI codes section.
/usr/bin/ssh $@
Start /usr/bin/ssh
with arguments provided.
Add the function to your ~/.bashrc
file (if you don't use bash
modify it to match your shell syntax and add it to your shell's startup file) so that it's always accessible.
Note that it will also set the terminal tab name when executing a remote command:
ssh user@server command
for example:
ssh jaume@myserver ls -l /