Xterm title

From LQWiki
Jump to: navigation, search

The window title of xterm and related programs -- such as gnome-terminal -- may be set through use of ANSI escape sequences.

Anything printed to the screen that is preceded by ESC]2; and followed by CTRL-G will become the xterm title.

In the shell

This shell function allows for a convenient method to set the title:

 settitle() {
     # ^[ is actually ESC, produced by typing (in vi) control-V ESC
     # ^G is actually CTRL-G, produced by typing (in vi) control-V control-G
     echo "^[]2;$*^G" 

Once defined (in .profile etc), this shell function may be used to set the title to anything desired. It is often used in the PROMPT_COMMAND variable (see shell prompt) to set the current directory, hostname, or other information as the xterm title. One example (in this author's .profile:)

   XTERM_TITLE="`hostname -s` : `basename $TTY` ($LOGNAME) "
   settitle "$XTERM_TITLE"

...which gives a title resembling "nyarlathotep : ttyp4 (matt)"

In vim

The vim editor can set the xterm title to include the filename and directory of each file you edit.

In your ~/.vimrc put:

set title

...which will cause vim to set the title according to its own predefined format string. Upon exit, vim will change the title to "Thanks for flying VIM!", as it has no way to know what the title was before you entered; but if you fix the title in your PROMPT_COMMAND it will immediately revert to your preferred title syntax.