Use PROMPT_COMMAND to set titles on iTerm tabs

Oct 20, '03 10:24:00AM

Contributed by: jtphil

I like to use iTerm for my terminal sessions because of its tabbed session interface. My preferred shell is bash. You can customize the title of each of your iTerm sessions either manually, using AppleScript, or with iTerm's bookmarks, but I like to let bash do it dynamically using the PROMPT_COMMAND variable. Create or edit the .bashrc file in your home directory and place the following line in it:


export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}: ${PWD/#$HOME/~}\007"'
(That should all be on one line.) The PROMPT_COMMAND holds the name of a program that is executed each time bash prints your command prompt. In this case, it is executing echo and having it send the escape sequence that sets the title bar text. The title bar and the iTerm tab will be set to your user name, the machine's host name, and the current directory. If the current directory is your home directory or a sub-directory beneath it, it will appear with the tilde notation. This works it Terminal too, but it's especially useful in iTerm when you have many tabs open.

I actually found the syntax for this PROMPT_COMMAND in Redhat Linux. In the recent Redhat distributions, this is the default for any xterm terminals. If you have iTerm terminal emulation set to "xterm-color" and log on to a Redhat Linux system, your PROMPT_COMMAND is automatically set to this echo command.

BTW, I believe it was covered in an earlier hint, but the default command prompt on OS X's bash is not very useful. While you are editing .bashrc you might want to add the following line.

export PS1="[\u@\h \W]\\$ "
[robg adds: I haven't tested this one...though with the apparent arrival of the bash default shell in Panther, I guess I should!]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031015173932306