Can I get terminal title? (or otherwise restore old one)
Asked Answered
C

4

14

Setting terminal title is easy with echo -e "\e]0;some title\007". Works with pretty much every terminal program.

What I want is to set terminal title when some program starts - and restore old one when it finishes. Is this possible?

Crick answered 12/7, 2010 at 22:5 Comment(0)
C
3

There are some terminal programs that supporting it (xterm has compile time options for that, as mentioned by RWS), but most terminal programs simply lack such feature, including in particular Terminal.app.

Crick answered 29/7, 2010 at 8:11 Comment(2)
For Terminal.app, AppleScript to the rescue: title=`osascript -e 'tell application "Terminal" to get name of front window'` and then echo $title works just as expected. Not too nice to need AppleScript though.Ruination
(And using AppleScript like above only works nicely if you only have a single Terminal window, or if it's indeed the frontmost window...)Ruination
S
12

On xterm, the terminal control sequences 22 and 23 work fine, as in

#!/bin/sh
/bin/echo -ne '\033[22;0t'  # Save title on stack
/bin/echo -ne "\033]0;$(date)\007"
sleep 1
/bin/echo -ne '\033[23;0t'  # Restore title from stack

It looks like this isn't supported in the Mac OS X Terminal.App though.

Scarlatti answered 18/11, 2013 at 13:21 Comment(5)
It have tested Konsole 1.6.4 and Gnome Terminal 2.16.10 (both very, very old, I have to admit, but nice to know if you want to go for compatibility). Result: It does not work.Sialoid
Also not working with roxterm (probably other VTE based terminals?)Audiogenic
Alacritty 0.7.2 (5ac8060b) works.Incommodity
Works in tmux. st seems to have a patch for this.Electorate
Works with Gnome Terminal 3.40.3, but not on Konsole 21.08.2Washrag
T
4

My solution was to set the window title during my script, then unset the window title when I completed. Unsetting the title reverted to the original value. Specifically, I did the following:

# Set the terminal title
printf "\e]2;%s\a" "running my script"
# Do whatever processing is required.
...

# Restore terminal title
printf "\e]2;\a"
Tanah answered 13/11, 2013 at 20:43 Comment(1)
This even works with Terminal.app, but it resets the title to the default value and not to the previous title before the change (which is good enough for me).Washedout
C
3

There are some terminal programs that supporting it (xterm has compile time options for that, as mentioned by RWS), but most terminal programs simply lack such feature, including in particular Terminal.app.

Crick answered 29/7, 2010 at 8:11 Comment(2)
For Terminal.app, AppleScript to the rescue: title=`osascript -e 'tell application "Terminal" to get name of front window'` and then echo $title works just as expected. Not too nice to need AppleScript though.Ruination
(And using AppleScript like above only works nicely if you only have a single Terminal window, or if it's indeed the frontmost window...)Ruination
A
2

Yes, that is possible indeed. See a xterm reference manual (like this for example) and wander your way through it. xterm even has a build in stack for this, so you don't have to store the title manually.

Abigail answered 23/7, 2010 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.