10

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?

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

5 Answers 5

12

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'

This will set the title to my title:

echo -ne '\e]0;my title\007'

And this will restore the prior window and icon titles:

echo -ne '\e[23t'
0
5

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

2
  • 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
    Commented Jan 7, 2012 at 19:02
  • 1
    Do note though that obtaining the title via the X protocol, which is what xtitle does, only works for local sessions.
    – ak2
    Commented Jan 7, 2012 at 20:07
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

Not sure why, but the command to change the title is a bit different for me. I have to use either:

  • echo -ne '\e]30;my title\007'
  • echo -ne '\033]30;my title\007' to set the title.

Why do I need to set a '3' in front of the '0' to change the title ?

I can't save/restore it. Is there other syntax for the echo -ne '\e[22t' command (I'm using Konsole, but I would like to adapt to most terminals).

Thanks,

-1

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/~}"'
3
  • 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
    Commented Jan 7, 2012 at 17:39
  • This would not work for cases where other apps are setting the title.
    – TheGeeko61
    Commented Jan 7, 2012 at 18:55
  • The $PROMPT_COMMAND is not the window title. They are unrelated. Commented Sep 26, 2019 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .