How can I make OSX terminal tab names the same as .ssh/config host aliases

3

1

Setting up a .ssh/config is a great time saver. The host aliases with user defined names make it easier keep track servers and key files. However, the OSX terminal tab names are displaying what ever the server says its host name is, instead of the host alias from the .ssh/config file.

For this entry:

Host CustName01
    HostName ec2-20-12-130-143.compute-1.amazonaws.com
    IdentityFile ~/.ssh/remote.pem
    User root

The OSX terminal tab name shows this:

root@domU-11-21-35-24-33-9A:~

I tried to solve the problem by setting the tab name before connecting:

Host *
    PermitLocalCommand yes

Host CustName01
    LocalCommand printf "\e]1;CustName01\a"
    HostName ec2-20-12-130-143.compute-1.amazonaws.com
    IdentityFile ~/.ssh/remote.pem
    User root

It sets the tab name before connecting, but then shows this again after connecting:

root@domU-11-21-35-24-33-9A:~

How can I get my tab to automatically show the CustName01 alias I specified in the ssh config file? Manually setting the value in the inspect tab dialog isn't what I'm after.

Executing the command to set the tab name while connected to a remote server does nothing:

printf "\e]1;CustName01\a"

tponthieux

Posted 2013-02-08T04:19:58.567

Reputation: 131

Answers

0

Kind of a hack, but this sort of works. I say sort of works because there are two places to set the title of the tab if you look in the tab inpector. I wasn't able to set the input I wanted that says "Tab Title" with applescript, but I was able to set the one that says "Window Title." This solution will help me know what machine I'm connected to though.

settabname.sh

#!/bin/bash

osascript -e 'delay 2' -e "tell application \"Terminal\" to tell window 1 to set custom title to \"$1\""

~/.ssh/config

Host *
    PermitLocalCommand yes

Host CustName01
    LocalCommand ~/.ssh/settabname.sh CustName01 &
    HostName ec2-20-12-130-143.compute-1.amazonaws.com
    IdentityFile ~/.ssh/remote.pem
    User root

The trick here is that & makes the bash with the embedded applescript execute asynchronously. The applescript has a delay which allows it to rename the tab after your remote connection does a rename.

Here's how you can clean up the tab name after the ssh command exits. https://unix.stackexchange.com/questions/40899/ssh-localcommand-on-exit

~/.bashrc

## run a cleanup command after ssh exit
ssh() {
    if command ssh "$@"; then
        # commands go here
        ~/.ssh/settabname.sh Local &
    fi
}

tponthieux

Posted 2013-02-08T04:19:58.567

Reputation: 131

This doesn't work for me at all. – Gordon – 2015-07-31T17:35:13.290