Networking HowTos
Networking HowTos

How to set the PuTTY window title from within Linux

January 23, 2012 Linux

PuTTY is a popular SSH client for a variety of operating systems. You can set up the window title in the configuration options, but sometimes you may want to display your own information in the window title text.
PuTTY lets you do this via escape commands. More specifically by using the ^[ (escape) and ^G (bell) escape commands.
The ^[ escape code in ansi format is “\033]0;”.
The ^G escape code in ansi format is “\007 “.
To set the window title to “testing” you would echo the “\033]0;” escape command, the word “testing”, and then the “\007” bell command.
Eg:

echo -ne "\033]0;testing\007"

Now, you might find that it doesn’t actually change the title. By default, some Linux distributions set up their own escape commands, to set the host name, and current path in the window title. This is fine for most users, but if you want a bit more control, you may find that any escape commands you run, don’t appear to update the title.
It is actually setting the title to your custom string, but straight after, it is setting it back what they set it to, so it looks like it doesn’t change.
This is due to them setting the prompt_command variable, (which runs after every command), or embedding it in the PS1 variable, which produces the prompt text.
Default “PROMPT_COMMAND” variable in CentOS:

printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"

Default PS1 variable in Ubuntu:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

Run either of the following commands to check how your distribution might be setting the window title:

echo $PROMPT_COMMAND

or

echo $PS1

To make the changes permanent, you will probably need to edit the .bashrc script in your home directory.
Below are some examples of setting the PuTTY window title:
Ubuntu:
Output the uptime and load average in the window title:

PS1="\[\e]0;`uptime`\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ "

Output the hostname in the window title:

PS1="\[\e]0;`hostname`\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ "

CentOS:
Output the uptime and load average in the window title:

PROMPT_COMMAND='printf "\033]0;Uptime: %s\007" "`uptime`"'

Output the hostname in the window title:

PROMPT_COMMAND='printf "\033]0;%s\007" "`hostname`"'

Output the logged in username, the hostname, and kernel details:

PROMPT_COMMAND='printf "\033]0;%s@%s - %s\007" "`whoami`" "`hostname`" "`uname -s -r`"'

More information can be found at the following links:
http://en.wikipedia.org/wiki/ANSI_escape_code
http://www.chiark.greenend.org.uk/~sgtatham/putty/

You Might Also Like