Friday, June 27, 2008

Setting the PuTTY window title from command line

I sometimes want to set the title of my PuTTY windows, like "Editor", "Compiler", etc. to identify distinct windows. I found the following script very useful. You can add that to your ~/.bash_profile. Once you login, you can set the title to whatever you want:

function wtitle {
if [ "$TERM" == "xterm" ] ; then
# Remove the old title string in the PS1, if one is already set.
PS1=`echo $PS1 | sed -r 's/^\\\\\[.+\\\\\]//g'`
export PS1="\[\033]0;$1 - \u@\h:\w\007\]$PS1"
else
echo "You are not working in xterm. I cannot set the title."
fi
}

The above function will make the window title to be whatever argument you give followed by the usual user@host:workingdirectory. I think this should work with any xterm client. Not just PuTTY. (I haven't tested with any other xterm client.)

For e.g. to set the window title to be Editor, you would give the following command:
wtitle Editor

4 comments:

josy said...

i stumbled upon your site (putty's window title change script).
pretty cool! - works perfekt!

regards from austria,
josy

Rosarin Roy said...

Glad it helped you. Thanks for dropping a word.

Kornelis said...

Hi,

thanks for the Code.
But since sed -r isn't a valid option on Solaris i just changed the code a bit an specified the PS1 Variable to my standard config before i added the text:

function wtitle {
if [ "$TERM" == "xterm" ] ; then
# Remove the old title string in the PS1, if one is already set.
#PS1=`echo $PS1 | sed 's/^\\\\\[.+\\\\\]//g'`
PS1='${LOGNAME}@${RECHNER}:$PWD > '
export PS1="\[\033]0;$1 - \u@\h:\w\007\]$PS1"
else
echo "You are not working in xterm. I cannot set the title."
fi
}

Best Regards

Kornelis

Stephen said...

Hey thanks a lot for posting to both Roy and Kornelis. That was nice and easy and now I don't have to randomly click around my different putty windows. That's great!