Sunday, March 21, 2010

Automatically update the Terminal window title in Mac OS X

It seems Mac OS X doesn't provide the nice Bash functionality found in RHEL/CentOS/Fedora Linux that updates the terminal title after each command. For example, using Linux, if I do the following:
cd ~/Developer
the window title is updated to:
jason@rocksteady:~/Developer
While the Terminal app in OS X does allow you to customize the title to some extent by adding the TTY name, name of running process etc. I prefer the 'user@host:pwd' format.

After digging around in /etc/bashrc on CentOS 5, I found you can set the PROMPT_COMMAND Bash variable and have your title updated after every command.

Add the following to your .bash_profile in Mac OS X to get the same behaviour:
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'

2 comments:

  1. Hey that's pretty neat. I've updated my terminal window on my mac to do the same.

    ReplyDelete
  2. This post title is misleading. You should say "How t set the prompt" rather than window title. Also, you should use PS1 variable to set the prompt.

    ReplyDelete