Last Updated: February 25, 2016
·
1.19K
· mcappadonna

Keep Terminal.app tab name follow working host

I've saw a great pro tip from joshbuhler

He have shared a way to change the Terminal.app tab name with an easy bash function.

After that, I've change a little my .bashrc to keep the updated name of the host I'm working on directly on the tab name.

Here is the magic:

function tabname () {
    # Change Terminal.app tab name (by josjbuhler)
    echo -en "\033];$1\007"
}

function sshname () {
    SSHHOST="${@: -1}"
    tabname "`echo $SSHHOST|cut -d'@' -f2`"
    unalias ssh
    ssh $1
    alias ssh=sshname
    tabname localhost
}

alias ssh=sshname
tabname localhost

Now, when I open a new tab, it's named localhost.
When I start connecting to a remote host with one of those commands:

# ssh root@myremote
# ssh mymailserver

The shell do those things:
1. Extract the remote hostname or IP from the arguments
2. Set the tab name to that
3. Unaliasing ssh (to avoid infinite loop)
4. Connect to remote host

When connection is done, the function will create the ssh alias, and set back the tab name to localhost.