Naming your Terminal tabs in OSX Lion

If you find yourself in the Terminal app with a bunch of tabs open, the default name of “bash” isn’t very useful when navigating between them. You can change the tab name via the UI by right clicking the tab, then clicking “Inspect Tab” and changing the window or tab names but this is somewhat long winded.

Below are a couple of bash functions I have in my “.profile” file to make this easier:


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

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

Now you can easily name your tabs or windows with the following:


# Rename tab
tabname "Funky Tab"

# Rename window
winname "Funky Window"

Thanks to Bubu and Chris Page on the SuperUser site for the right codes.

15 thoughts on “Naming your Terminal tabs in OSX Lion”

  1. I’ve even put this into my PS1, so if any program changes it (e.g. VIM), when execution returns to shell, the title will be restored.

    export PS1=”\[\e]1;${XTERM_TITLE:-\u@\h:\w}\a\]\u@\h:\w”

    title() {
    export XTERM_TITLE=”$@”
    }

    Don’t forget to surround your title ASCII sequence inside PS1 with \[ \] pair or you will get wrong command prompt display in some cases.

  2. CMD + I also gets you the inspector window, pressing CMD + I again closes the window or you can press CMD + W as usual.

  3. It’s Ok.
    But after I ssh to another server, it’s replaced and then I cannot edit it again.
    How can I keep previous terminal title after ssh?

  4. Awesome! I’ve been looking for this for a few months and just never found it for some reason…. Add PS1 to my google search got me to your site… Now as was point out above, the function is location of the computer one is initially opening the terminal….. without copying this function to every box….. is there a way to set the tab and by grabbing the hostname and or other info and piping that back to the originating terminal session so that the tabname can be changed? or is one limited to setting the tabname and then logging into the box….

    Example:
    Tabname “The local_hostname of the originating terminal session”
    >ssh another_server
    >>hostname piped back to originating terminal tab’s name
    >>hostname > local_hostname::tabname
    end result is tabname of “another_server”

    Short of wrapping calls …. is there a way….. that’s my quest….

  5. Hmm, that sounds a little complicated. I just use this to name my tabs locally and keep track of them in local development.

  6. Cool!
    Added to my ssh script – so each tab names itself (handy when working between Lab and prod), saves me having to connect and manually name each tab (my script does all that so I just need to enter the password !).

    Thank you

  7. to stop my ssh connections from renaming the tab I execute this script in the background on tabs prompt:

    #!/bin/sh
    while true
    do
    printf “\e]1;$1\a”
    sleep 10
    done

    Usually I name the tab for the ssh connection it will be making.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>