Friday, April 13, 2007

Change the title of xterm

In most modern Linux distribution, the title of terminal follows the working directory. The mechanism behind this feature is not simple. You can even customize the title. Please note that, in this article, it is assumed to use xterm and bash.

In xterm, the following escape sequence can change the title of terminal windows:
ESC]0;title_stringBEL
where ESC and BEL is 033 and 007 (in octal ASCII) respectively. For example, we can use the following echo command to set the terminal title as "Hello World.":
echo -ne "\033]0;Hello World.\007"

However, if you issue the above command in your terminal, the title may not be changed. It is because the PROMPT_COMMAND environment variable have already been defined for changing the terminal title. The value in PROMPT_COMMAND environment variable will be executed as command prior to issuing each prompt. In my terminal, the value of PROMPT_COMMAND is:
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"

In the above command, ${USER} and ${HOSTNAME} is the username and hostname respectively, while the ${PWD/$HOME/~} is the current working directory with $HOME abbreviated as ~. Therefore, in my terminal, the title will be changed to show username, hostname and current working directory, e.g.:
ahlam@oxygen: ~/download

To customize your terminal title, you should update the value of PROMPT_COMMAND, e.g.:
export PROMPT_COMMAND='echo -ne "\033]0;Hello World.\007"'


Reference:
How to change the title of an xterm
man bash

2 comments:

Aamer Akhter said...

in ubuntu 10.04 there doesn't seem to be the PROMPT_COMMAND env var used. But the behavior with the window title (pwd) is still the same.

Sravan Reddy said...

You are awesome.. Searched every where. Got it here only. Thank you very much.