How to use SSH host names for tabs in the OS X Terminal

The OS X Terminal supports tabs, but its default naming convention may be confusing at times.

While the OS X Terminal is not a familiar operating environment for most Mac users, those who have had experience with Unix-based systems will find it to be an invaluable tool for managing their systems. One of the most common uses of the Terminal is to establish a remote log-in session on a system using the secure shell (SSH) command, and sometimes invoke multiple connections to the same system and different systems at the same time.

As a result of this, you might find you have many windows open at once to manage these connections in, though starting with Snow Leopard Apple added the ability to group Terminal sessions in tabs, which allows for better management of multiple Terminal windows and the various tasks you are performing.

Terminal tab names in OS X
The tab names will only show up as "ssh," regardless of any other specifics about the connection. Screenshot by Topher Kessler/CNET

While convenient, tabs unfortunately have a drawback in that they obscure all Terminal windows except for the active tab, which in the case of SSH connections introduces an added difficulty when managing your tasks. While tabs are named after the active command being run, this doesn't help, as regardless of what server is being connected to with SSH, the tab will simply be named "ssh."

There are several approaches you can take to getting around this problem:

Terminal inspector
The inspector can be used to set the active tab's name. Screenshot by Topher Kessler/CNET
  1. Manually name the tab
    The OS X Terminal supports custom names for individual tabs, which can be set using the inspector for each tab. To do this, either right-click the desired tab and choose "Inspect Tab," or activate it and press Command-I. In the inspector window that appears, go to the Info tab where you will see a field for setting the tab name. This option is best if you have a relatively long-term and one-time project you are working on.
  2. Set a function to name the tab
    If you regularly need to rename tabs, then accessing the inspector window can be inconvenient; however, the folks at The Lucid found a clever way to name tabs, which can be set in a bash function or script to change the current tab's name more quickly. To do this, open the Terminal and run the following command to edit the bash profile:

    pico ~/.bash_profile


    Once the profile is open, go to the bottom of it using the arrow keys and enter the following function (copy and paste it for convenience):

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


    After this has been added to the profile, press Control-O to save and Control-X to quit the editor. Now when you type the following command you will invoke the function and name the current tab accordingly:

    tabname NAME


    Note that while this is defined as a function, you can also place the "printf" line in a shell script called "tabname," alias or move it to an included path such as /usr/bin/, and then be able to access it in a similar manner (though this has the benefit of allowing scripting of the function).

    With this command set up, you can run it in-line before additional commands in the following manner to name the tab while you run a command:

    tabname NAME; ssh username@host

  3. Script the naming routine
    The above options are convenient to have around, but they aren't very dynamic since you have to explicitly invoke them whenever you want to name a terminal session. However, with the use of a small shell script you can set up commonly used commands such as SSH to automatically name the current tab accordingly. To do this, create a new shell script by running the following command in the Terminal:

    pico ~/myssh.sh


    With the editor open, add the following lines to the script (copy and paste them to ensure all characters are appropriately included):

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


    This script uses the same approach that The Lucid found, but enhances it in a couple of ways. First it passes arguments for SSH to the "printf" command and parses them (using the string editor "sed") to pull out the hostname from the arguments string. It then passes these same arguments to the standard "ssh" command, to allow it to run. When the SSH command is complete and quits, the script then renames the tab to its original "bash" shell name.

    As mentioned previously, save the file and quit the editor by pressing Control-O followed by Control-X; however, be sure to allow execution of the script by running the following command:

    chmod u+x ~/myssh.sh


    With this done, you can now execute the script in the same way that you would invoke the SSH command (including additional SSH arguments), similar to the following, and then have the tab be renamed to that of the host address:

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


    As a last note, you might consider moving or aliasing the script to the hidden /usr/bin/ directory to have it automatically included in the default bash paths. This will allow it to be run from anywhere, instead of it being necessary to keep it in your home directory (or wherever you wish to store it) and then target it using the tilde home folder reference.


Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    HOT ON CNET

    Want affordable gadgets for your student?

    Everyday finds that will make students' lives easier: chargers, cables, headphones, and even a bona fide gadget or two!