Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use PROMPT_COMMAND to set titles on iTerm tabs UNIX
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!]
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)

Use PROMPT_COMMAND to set titles on iTerm tabs | 13 comments | Create New Account
Click here to return to the 'Use PROMPT_COMMAND to set titles on iTerm tabs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use PROMPT_COMMAND to set titles on iTerm tabs
Authored by: kal on Oct 20, '03 11:24:55AM

I do the same thing with the PS1 variable:
PS1="\[\033]0;\u@\h: \w\007\]\u@\h:\W>"

Does anyone know which is the best/recommended method to use? Setting PROMPT_COMMAND or PS1 ?

[ Reply to This | # ]
use this for zsh
Authored by: sapporo on Oct 20, '03 11:56:04AM
function precmd(){ echo -n "^[]0;$PWD^G" }
Note that ^[ is actually the esc character and ^G is ctrl-G. To enter these characters in zsh, use the following sequences for each (as mentioned here):
^[   ctrl-v followed by esc  
^G   ctrl-v followed by ctrl-g  

[ Reply to This | # ]
I use this little shell script
Authored by: semios on Oct 20, '03 06:29:03PM
Use the instructions in the parent comment to insert the "^[" and the "^G". Or just download it here and don't bother trying to coerce vi into making funny control characters.
# title
# Shane Celis <>

if [ $# -eq 0 ] || [ "$1" == "-h" ]; then
	echo "usage: title <string>" >&2
	echo "sets the title for the terminal window" >&2
	exit 2;
echo -n "^[]0;$title^G"

[ Reply to This | # ]
use this for zsh
Authored by: juggularity on Feb 24, '09 11:43:21AM
You can use something like this instead
function chpwd(){ echo -ne "e]0;$PWD}a" }
which is cut/paste safe. I prefer having just the current directory displayed instead of the whole pathname. To do this replace

[ Reply to This | # ]
to set titles in tcsh or csh
Authored by: Apollo18Pnut on Oct 20, '03 01:30:06PM
This sets the title of any terminal window when you cd (or pushd or popd), instead of when the prompt is displayed, but it's basically the same idea. I have this in my ~/.cshrc file.
Note that "^[" is the ESC character, and "^G" is CTRL+G.

if ($?TERM) then
 switch ($TERM)
 case "Eterm*":
 case "xterm*":
  alias settitle 'echo -n "^[]2;$cwd^G"'
  alias cd 'cd \!*;echo $cwd;settitle'
  alias pushd 'pushd \!*;settitle'
  alias popd 'popd \!*;settitle'
  alias wintitle 'echo ^[]0\;\!*^G'

[ Reply to This | # ]
Use PROMPT_COMMAND to set titles on iTerm tabs
Authored by: Greedo on Oct 20, '03 03:46:09PM
This seems to work on iTerm shells I open on my own machine, but any shells I open on other machines don't take it. Specifically, I have an entry in my bookmarks that has:
   Name: foo
   Command: ssh
   Terminal: xterm
Any solutions to this?

[ Reply to This | # ]
Use PROMPT_COMMAND to set titles on iTerm tabs
Authored by: jtphil on Oct 20, '03 06:08:04PM

You need to edit your .bashrc file on and any other hosts that you log in to, unless the login scripts already set PROMPT_COMMAND for you.

[ Reply to This | # ]
PROMPT_COMMAND is slow. Here's another way.
Authored by: dsandler on Oct 20, '03 04:00:03PM
Using PROMPT_COMMAND this way can be slow, because every time bash displays a prompt it must now launch echo in a sub-process. Fortunately, since in this case PROMPT_COMMAND is just being used to echo bytes to the terminal, you can avoid it entirely by prepending those bytes onto your PS1. bash won't interpolate ${variable}s when it shows PS1, so you can't do everything you can in a PROMPT_COMMAND, but it has backslash-escapes for everything we need here.

Here's an example, replicating the effect of the original tip:

export PS1="\[\033]0;\u@\h: \w\007\][\u@\h \W]\$ "
The \[...\] sequence tells bash that the characters between the brackets will not change the position of the cursor in the terminal; without them, text wrapping at the prompt will get very confused.

[ Reply to This | # ]
Corrected code for above comment
Authored by: dsandler on Oct 20, '03 04:05:20PM

[Whoa, that was weird -- it looked fine in the preview. I'll try "Plain Old Text" this time.]


export PS1="\[\033]0;\u@\h: \w\007\][\u@\h \W]\$ "

[ Reply to This | # ]
another option
Authored by: wgscott on Oct 20, '03 05:29:52PM
Here's how to set the title bar and tabs separately in any of the shells:

[ Reply to This | # ]
Use PROMPT_COMMAND to set titles on iTerm tabs
Authored by: MordEth on Jan 29, '04 07:47:03AM

i can't believe anyone hasn't already posted this, but $PROMPT_COMMAND is not specific to iTerm (works in X-windows, as well as Apple's Terminal in 10.3), nor is it the same thing as $PS1.

$PS1 is the prompt that you have, whereas $PROMPT_COMMAND is the window title. they are not interchangeable. personally, i'm glad Apple finally decided to implement this in 10.3, as i really like titling my windows with $PROMPT_COMMAND and the keyboard shortcut to activate that window (i.e. Cmd-1, 2, etc.)

[ Reply to This | # ]
Use PROMPT_COMMAND to set titles on iTerm tabs
Authored by: corti on Nov 29, '04 05:05:46PM

Just to point out that the PROMPT_COMMAND variable does not set the windows title. It is a command that is executed before before issuing a prompt.

In the original hint the PROMPT_COMMAND contains an echo command which is the command actually changing the title.

You can use PROMPT_COMMAND to do whatever you want. An example could be to print out the date before the prompt:
export PROMPT_COMMAND=date


[ Reply to This | # ]
Alternate PS1 format, and solving resize problems in bash
Authored by: razzed on Apr 21, '09 09:13:00AM

I've found that when I resize the window in bash and iTerm, a lot of the times the shell will get confused and not figure out the window size correctly.

This usually manifests itself when typing out long lines, or using readline up and down to see previous commands. Rather annoying, up until today.

Often what I'll do is then do a

# TERM=xterm

or whatever and it will then figure it out.

With your above tip, I've fixed this issue by just setting the TERM on each prompt command. Not sure if this is the best way to do this, but it seems to work better and I'm not getting prematurely wrapped lines in bash anymore.

PROMPT_COMMAND='echo -ne "33]0;${USER}@${HOSTNAME%%.*}: ${PWD/#$HOME/~}07"; export TERM=xterm-color'

And, completely unrelated, a decent, Color Bash Prompt:

PS1='[e[34;1m]u@[e[31;1m]h [e[0m]w[e[0m] > '

Cobbled from various other sources, gives:

user@host cwd > _


Kent Davidson
MarketRuler: Marketing Power Tools

[ Reply to This | # ]