How to change the title of an xterm: Printing the current job name

Next Previous Contents

## 5. Printing the current job name

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`.

## 5.1 zsh

`zsh` provides an ideal builtin function for this purpose:

> `

>     preexec()   a function which is just before a command is executed
>     $*,$1,...   arguments passed to preexec()

> `

Thus, we can insert the job name in the title as follows:

> `

>     case $TERM in
>         xterm*)
>           preexec () {
>             print -Pn "\e]0;$*\a"
>           }
>         ;;
>     esac

> `

Note: the `preexec()` function appeared around version 3.1.2 of `zsh`, so you may have to upgrade from an earlier version.

## 5.2 Other shells

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.

Next Previous Contents