7

This LDP Howto demonstrates how to change the title of an xterm.

I know that this is also possible using the xtitle command.

Before invoking a long-running action, my script uses such techniques to change the title of its xterm window.

I would like to extract the name of the xterm window prior to changing it so that, once the long-running action is completed, I can restore the title to its previous value.

It would sort of be an inverse to xtitle... Is this even possible?

  • Related stackoverflow question. It looks like xterm has a stack for window title, if you can figure out how to use it. – jw013 Jan 7 '12 at 3:36
4

Use xtitle script.

There are many variants on internet, however, I tend to use one which depends on xprop

http://www.shelldorado.com/scripts/cmds/xtitle

  • Excellent! This is EXACTLY what I was hoping for... and it not only works for xterm but (unlike other solutions) also works for gnome-terminal. Thank YOU! – TheGeeko61 Jan 7 '12 at 19:02
  • Do note though that obtaining the title via the X protocol, which is what xtitle does, only works for local sessions. – ak2 Jan 7 '12 at 20:07
7

There is a control sequence for querying the current title, but it's usually disabled for security reasons. That's because it allows a malicious program or even just a catted text file to insert arbitrary character sequences as if typed on the keyboard, by first setting the title and then asking to have it reported back.

Hence the title stack was introduced instead. The following command will save the current window and icon titles onto the stack:

echo -ne '\e[22t'

And this one will restore them:

echo -ne '\e[23t'
2

For the trivial, (and probably most common case), where one might wish to query the prompt for the current xterm, simply use xprop -id $WINDOWID WM_NAME. If you want to extract the value for manipulation in a shell script, you can do something a bit more expensive such as:

   curtitle=`xprop -id $WINDOWID WM_NAME|awk '{print $3}'|xargs echo`

Using bash alone (to avoid the invocation of awk and xargs):

   curtitle=`xprop -id $WINDOWID WM_NAME`
   curtitle=`eval echo ${x##*=}`

The xargs echo in the first example and the eval in the second example is simply there to strip off the quotes that xprop puts around the value.

Replace WM_NAME with WM_ICON if you want to obtain the icon name rather than the string in the title-bar.

0

If I understand your question right, you should be able to get the current xterm title by inspecting the $PROMPT_COMMAND variable, e.g:

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

and you can set it like (linebreak for readability):

PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'printf "\033]\
0;%s@%s%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
  • The PROMPT_COMMAND is not the most authoritative way to get the title since it is not always set, and any command you run could change the title between prompts. For the most accuracy, terminal emulator specific control sequences are probably the way to go. – jw013 Jan 7 '12 at 17:39
  • This would not work for cases where other apps are setting the title. – TheGeeko61 Jan 7 '12 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.