I am new to using Mac (Mac Pro, OS X 10.8.4) and I connect to many remote hosts using ssh. I have created aliases in .ssh/config file and would like to have the host alias as the name of the tab in terminal.
Can someone please advise on how it can be done?
Thanks much.
Best Answer
EDIT: On Yosemite (OS X 10.10) use this instead:
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 toa.b.c.d
). Other combinations like-l user server
won't work properly.You can unset the function with:
You can create a function named
ssh
that sets the terminal tab name and then invokes/usr/sbin/ssh
:servername=$(echo $@ ...
The first line extracts the server name from the arguments passed to
ssh
. I tested it against several possiblessh
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 usebash
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:
for example: