Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Terminal Tab Names (SSH host)

I am trying to change the termnal tabs to show the remote hostame that I am connecting to with SSH. I have tried a few exports, and have not found the one that will provide the remote hostname in the tab.

MacBook Pro (15-inch Mid 2010), Mac OS X (10.7.4)

Posted on Jul 18, 2012 6:22 AM

Reply
9 replies

Jul 18, 2012 7:11 AM in response to mtharpe

The tab names are determined by the currently running process, and there is no way to automatically pass the hostname or any other information these processes are handling to the shell as a replacement for the process name. However, you can rename the tabs manually by right-clicking them and choosing "Inspect Tab" or by pressing Command-I. In the inspector window, click the "Info" tab and set the title in the appropriate text field (second from the top).

Jul 18, 2012 8:05 AM in response to mtharpe

Here's a solution that might work.


Open a terminal window and edit your bash profile:


     pico ~/.bash_profile


In the profile, add the following function:


     function tabname {
          printf "\e]1;$1\a"
     }


Save and quit the editor (control-o, control-x), and then create a new bash session. Now when you type the following command you can change the name of the tab:


     tabname NAME


(credit to The Lucid for this: http://thelucid.com/2012/01/04/naming-your-terminal-tabs-in-osx-lion/)


With this setup, you can add this command before your ssh command in the following manner:


     tabname NAME; ssh username@host


You can also script this behavior in the following manner. First create a script called "myssh.sh" or something similar, and then add the following to it:


     !#/bin/bash
     printf "\e]1;`echo $1 | sed -Ee 's/^.+\@//'`\a"
     ssh $1
     printf "\e]1;bash\a"


When you run the script, use it as a replacement for the ssh command (if you need to add arguments, then encase them in quotes or escape the spaces between them). For example, see below:


     ./myssh.sh "-p 22 username@host.com"


or...


     ./myssh.sh -p\ 22\ username@host.com


If there are no arguments then you can simply use the following:


     ./myssh.sh username@host.com


One issue with this script is that if you cancel the connection or otherwise kill the command without doing so cleanly, then the script will halt and leave the name of the tab as the hostname. The last line of the script should rename the tab back to "bash" when done if the ssh session exits cleanly and allows the script to continue.

Dec 13, 2012 1:29 PM in response to mtharpe

When I implement bash scenarios like these, I find that my SSH sessions, regardless, changes to the name user@host


So, first it changes to my_custom_tab_name and then it almost-immediately changes to user@host


I get the impression that SSH is sending the same control string when it runs, replacing the one I just sent.


I don't think it is the remote server, since when I run this command on the remote, it does not change my local tab name.


I looked in the SSH man pages and cannot find any documentation of it sending the escape code to change the terminal tab name.


Does anyone know how to control this behaviour?

Dec 14, 2012 9:13 PM in response to mtharpe

The following bash script does the following:

  1. Takes a hostname as a command line argument. Prompts for it if missing.
  2. Uses a trap for command errors. Reports them, and resets Terminal tab name back to bash
  3. Sets the terminal tab to the remote hostname
  4. Runs ssh
  5. On conclusion of normal connection, Terminal tab name is reset to bash


Assumption: In Terminal > Preferences > Settings > Window > Title. All items are deselected.


There is no need to do anything on the remote host to set terminal tab name.


User uploaded file

Dec 14, 2012 9:58 PM in response to VikingOSX

If ssh is not the changing the title, there is no need to have the remote node do anything.


However if ssh is changing the title, or the remote host's /etc/profile, then either the remote account's shell initialization needs to take control, or play some local games with a background process started before the ssh sleeping just long enough to allow ssh or remote system to do its thing, then wake up and reset it to what you want.

Terminal Tab Names (SSH host)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.