This document explains how to use escape sequences to dynamically change window and icon titles of an xterm. Examples are given for several shells, and the appendix gives escape sequences for some other terminal types.
1. Where to find this document
2. Static titles
3. Dynamic titles
4. Examples for different shells
5. Printing the current job name
6. Appendix: escapes for other terminal types
7. Appendix: examples in other languages
8. Credits
This document is now part of the Linux HOWTO Index and can be found at http://sunsite.unc.edu/LDP/HOWTO/mini/Xterm-Title.html. The latest version can always be found in several formats at
http://www.giccs.georgetown.edu/ric/howto/Xterm-Title/. This document supercedes the original howto written by Winfried Truumlmper.
Many people find it useful to set the title of a terminal to reflect dynamic information, such as the name of the host the user is logged into, the current working directory, etc.
Window and icon titles may be changed in a running xterm by using XTerm escape sequences. The following sequences are useful in this respect:
where ESC is the escape character (\033), and BEL is the bell character (\007).
Printing one of these sequences within the xterm will cause the window or icon title to be changed.
Note: these sequences apply to most xterm derivatives, such as nxterm, color-xterm and rxvt. Other terminal types often use different escapes; see the appendix for examples. For the full list of xterm escape sequences see the file ctlseq2.txt, which comes with the xterm distribution, or xterm.seq, which comes with the rxvt distribution.
should produce a title like username@hostname, assuming the shell variables $USER and $HOST are set correctly. The required options for echo may vary by shell (see examples below).
For information that may change during the shell's lifetime, such as current working directory, these escapes really need to be applied every time the prompt changes. This way the string is updated with every command you issue and can keep track of information such as current working directory, username, hostname, etc. Some shells provide special functions for this purpose, some don't and we have to insert the title sequences directly into the prompt string. This is illustrated in the next section.
Below we provide an set of examples for some of the more common shells. We start with zsh as it provides several facilities that make our job much easier. We will then progress through increasingly difficult examples.
In all the examples we test the environment variable $TERM to make sure we only apply the escapes to xterms. We test for $TERM=xterm*; the wildcard is because some variants (such as rxvt) can set $TERM=xterm-color.
(In our opinion this is just one of many reasons not to use C shells. See Csh Programming Considered Harmful for a useful discussion).
The examples below should be used by inserting them into the appropriate shell initialisation file; i.e. one that is sourced by interactive shells on startup. In most cases this is called something like .shellrc (e.g. .zshrc, .tcshrc, etc).
There are many more expansions available: see the zshmisc man page.
This could also be achieved by using chpwd() instead of precmd(). The print builtin works like echo, but gives us access to the % prompt escapes.
which sets a prompt of "tcsh% ", and an xterm title and icon of "username@hostname: directory". Note that the "%{...%}" must be placed around escape sequences (and cannot be the last item in the prompt: see the tcsh man page for details).
bash supplies a variable $PROMPT_COMMAND which contains a command to execute before the prompt. This example sets the title to
where \033 is the character code for ESC, and \007 for BEL.
Note that the quoting is important here: variables are expanded in "...", and not expanded in '...'. So $PROMPT_COMMAND is set to an unexpanded value, but the variables inside "..." are expanded when $PROMPT_COMMAND is used.
case $TERM in xterm*) PS1="\[\033?0;\u@\h: \w\007\]bash\\$ " ;;
PS1="bash\\$ " ;; esac
Note the use of \[...\?, which tells bash to ignore the non-printing control characters when calculating the width of the prompt. Otherwise line editing commands get confused while placing the cursor.
ksh provides little in the way of functions and expansions, so we have to insert the escape string in the prompt to have it updated dynamically. This example produces a title of username@hostname: directory and a prompt of ksh$ .
case $TERM in xterm*) HOST=`hostname` PS1='^[?0;${USER}@${HOST}: ${PWD}^Gksh$ ' ;;
- )
PS1='ksh$ ' ;; esac
Note that the ^[[ and ^G in the prompt string are single characters for ESC and BEL (can be entered in emacs using C-q ESC and C-q C-g).
where we have had to alias the cd command to do the work of sending the escape sequence. Note that the ^[[ and ^G in the string are single characters for ESC and BEL (can be entered in emacs using C-q ESC and C-q C-g).
Notes: on some systems hostname -s may be used to get a short, rather than fully-qualified, hostname. Some users with symlinked directories may find `pwd` (backquotes to run the pwd command) gives a more accurate path than $cwd.
Often a user will start a long-lived foreground job such as top, an editor, an email client, etc, and wishes the name of the job to be shown in the title. This is a more thorny problem and is only achieved easily in zsh.
Note: the preexec() function appeared around version 3.1.2 of zsh, so you may have to upgrade from an earlier version.
This is not easy in other shells which lack an equivalent of the preexec() function. If anyone has examples please email them to the author.
Many modern terminals are descended from xterm or rxvt and support the escape sequences we have used so far. Some proprietary terminals shipped with various flavours of unix use their own escape sequences.
aixterm recognises the xterm escape sequences.
These terminals set $TERM=iris-ansi and use the following escapes:
For the full list of xwsh escapes see the xwsh(1G) man page.
The Irix terminals also support the xterm escapes to individually set window title and icon title, but not the escape to set both.
cmdtool and shelltool both set $TERM=sun-cmd and use the following escapes:
These are truly awful programs: use something else.
dtterm sets $TERM=dtterm, and appears to recognise both the standard xterm escape sequences and the Sun cmdtool sequences (tested on Solaris 2.5.1, Digital Unix 4.0, HP-UX 10.20).
hpterm sets $TERM=hpterm and uses the following escapes:
int main(int argc, char *argv[?) {
printf("\033&f0k%dD%s", strlen(argv[1?), argv[1?); printf("\033&f-1k%dD%s", strlen(argv[1?), argv[1?); return(0);
}
It may be useful to write a small program to print an argument to the title using the xterm escapes. Some examples are provided below.
int main (int argc, char *argv[?) {
printf("%c]0;%s%c", '\033', argv[1?, '\007'); return(0);
}
print "\033]0;@ARGV\007";
Thanks to the following people who have provided advice, errata, and examples for this document.
No other page links to HowToXtermTitle yet.
lib/main.php:944: Notice: PageInfo: Cannot find action page