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
Update on Jul 15, 2013: I wrote a simpler version of this function which can be found here

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!
mick said…
great tip. thx.
regis_ter said…
Just I need. Perfect.
Moltes grĂ cies.

Joan
Anonymous said…
The new modified title dosent show up if u are in `srceen` in putty
davidm said…
Thanks! Very useful and titled well so search finds it easily.
Sanjay said…
Hello Roy,
Yes it indeed works well and solves one of the irritating problems I was facing. Thanks

Sanjay
tonyboy666 said…
Actually, you just have to force the terminal in your session settings to xterm instead of default value which must be vt100 !!!

That simply did the job for me ;-)

This thread kinda put me on the good path and trail.

Thx
Hoser said…
This work very nicely when used with

PROMPT_COMMAND="wtitle \${WINDOW_TITLE:-\\\W}"

You can set the WINDOW_TITLE yourself, but it you don't, it will use the DIR you are currently in. Very handy!
Tgr said…
This drops trailing whitespaces. The solution I came up with (admittedly very ugly, I am not a shell guru):

PS1=`echo X${PS1}X | sed -r 's/^\\\\\[.+\\\\\]//g' | sed 's/^.//' | sed 's/.$//'`
Anonymous said…
This is nice. I was looking for it for some time now.
btw, is there a similar way to change the title in ksh instead of bash?
Possible values and instances have developed around all those possible objects and meanings for the students and surely by the time the ideas would amount to better understanding to regard about all those.

Popular posts from this blog

Note on allocationSize parameter of @SequenceGenerator while using JPA

The mysterious ORA-03111 error

Creating a Collection with a single element