Name terminal windows from inside them

Mar 29th, 2008 @ 6:45 pm CST

Found myself with some rare free time, queuing up some small posts. Here’s a bash function I’ve been using for about a year now to name terminal windows. This is helpful to organize the 10-20 terminals I might have open at any given time across many virtual desktops. Unlike some tricks, I actually use it all the time so I figure that’s one person that finds it useful… so here it is in case someone else might also find it useful.

From .bashrc, it is very simple:

function n() {
  if [ "X" = "X$1" ]; then
    echo "give at least one parameter for window name"
    return 1
  fi

  NAME="$*"
  
  PROMPT_COMMAND=’echo -ne “\033]0;$NAME\007″‘
  export PROMPT_COMMAND
  return 0
}

Here is the ASCII version for copy/paste to avoid unicode issues etc.

Nothing to it… just names the current terminal window.

$ n CLUSTER 3
$ ssh tfreeman@cluster3...

I like CAPS usually and I set my title bar font all big:

This overrides the default setting which was to print the current directory. Here’s how to do that…

PROMPT_COMMAND='echo -ne "\033]0;${PWD/$HOME/~}\007"'

    One Response to “Name terminal windows from inside them”

  1. Von Says:

    Interesting, I’ve always used a ‘2′ instead of a ‘0′ for the window tittle (immediately after the ‘]’). Not sure what the difference is.

    If you use a ‘1′ that changes the icon name under X11 BTW.

    I use the following alias under tsch to change the title on demand:
    alias xtitle echo -n ‘”33]1;\!*0733]2;\!*07″‘