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:
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:
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 EditorUpdate on Jul 15, 2013: I wrote a simpler version of this function which can be found here.
Comments
pretty cool! - works perfekt!
regards from austria,
josy
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
Moltes grĂ cies.
Joan
Yes it indeed works well and solves one of the irritating problems I was facing. Thanks
Sanjay
That simply did the job for me ;-)
This thread kinda put me on the good path and trail.
Thx
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!
PS1=`echo X${PS1}X | sed -r 's/^\\\\\[.+\\\\\]//g' | sed 's/^.//' | sed 's/.$//'`
btw, is there a similar way to change the title in ksh instead of bash?