How to change Xterm titles

I should know this by heart but ...

Window and icon titles may be changed in a running xterm by using XTerm escape sequences. The following sequences are useful in this respect:

  • ESC]0;<b>string</b>BEL -- Set icon name and window title to string
  • ESC]1;<b>string</b>BEL -- Set icon name to string
  • ESC]2;<b>string</b>BEL -- Set window title to string

where ESC is the escape character (\033), and BEL is the bell character (\007).

echo -n "\033]0;${USER}@${HOST}\007"

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

The following short script should help setting the title to an arbitrary value:

#!/bin/csh

if ( "$1" != "") then
   /bin/echo "Setting title to [$1]"
   /bin/echo -ne "\033]0;"$1"\007"
   /bin/echo -ne "\033]1;"$1"\007"
endif