How can I automatically update the title in an xterm running screen?

14

3

This is a bit of a followup to this question. I'm working in tcsh within GNU screen in an xterm.

I have the following in my .cshrc:

alias res_t 'xtset -t %h:%d "(%u:%g)" %e'       # reset titlebar
res_t                                           # reset title right now
alias precmd res_t

And this works fine!

However, when I run screen I see that the title doesn't get updated with the current directory. How can I make screen update the xterm title?

Nathan Fellman

Posted 2010-04-13T09:15:07.503

Reputation: 8 152

Have you found a solution that worked for you yet? – Trey Hunner – 2010-05-16T03:48:41.237

No. I tried the suggested answers but they didn't work. That's why I neither accepted them nor upvoted them. – Nathan Fellman – 2010-05-16T06:17:22.737

Answers

4

You can set the xterm window title by adding this to your .tcshrc or .cshrc (replace your current precmd alias):

alias precmd 'echo -n "\033]0;${PWD}\a"'

Make sure this is the only precmd alias in your .cshrc and .tcshrc files when using this. This method displays the current directory as the window title for me in both xterm a gnome-terminal regardless of whether a GNU screen session is open.

My answer was based on this.

Trey Hunner

Posted 2010-04-13T09:15:07.503

Reputation: 1 825

3

I understand you're using tcsh so this probably won't work... Just in case anyone is looking for the bash way to do this.

If your systems (local / remote) are running bash then you can use the "PROMPT_COMMAND" environment variable to set the window title. PROMPT_COMMAND is eval'd before a prompt is displayed.

(in your .bashrc):

export PROMPT_COMMAND='history -a && echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007'

Explanation:

'history -a'

This sets the shell to append to the history file every time a command is completed, rather than when the whole shell is completed. (This is not related to this example).

'echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'

This echoes the escape code "\033]0;" which sets the window title with "user" @ "host" variables (removing longest match from right to first ".") and then the working directory (substituting '~' for '$HOME').

The above trick will work with any terminal application that supports dynamic changing of window title (which iTerm, Terminal.app, urxvt, aterm, eTerm. xterm etc all do). You can also look into the shell variable "TITLEBAR" in the bash documentation, it is similar.

wawawawa

Posted 2010-04-13T09:15:07.503

Reputation: 249

1this will not work when inside gnuscreen. at least it is not working for me. thats why im here :) – gcb – 2014-09-16T23:23:32.953

Does this help? http://aperiodic.net/screen/faq#how_can_screen_use_xterm_s_title_bar

– wawawawa – 2014-09-17T09:20:28.707

2

This should work in any shell, but it's a dirty .screenrc hack. It takes over the your hardstatus line to work, which may be unacceptable to some users. But it works in gnome-terminal on the latest Ubuntu, even without the termcapinfo line below. Some situations may require deeper tweaks (I haven't tested on PuTTY, for example).

# enable xterm title setting; may not be necessary on some platforms
termcapinfo xterm*|rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

# dirty hack: put xterm title escapes in the hardstatus
# this example will yield "user@host || screen Win#:(windowtitle)
defhstatus "$USER@^EH || screen ^E:(^Et)"
# now turn it off so it doesn't print in the hardstatus line
hardstatus off

# and finally, use caption as a replacement hardstatus
caption always '%{= kG}[%{G}%H%{G}][%= %{= kw}%?%-Lw%?%{R}(%{W}%n*%f%t%?(%u)%?%{R})%{w}%?%+Lw%?%?%= %{G}][%{B}%C%a %M.%d%{G}]'

Found at this link.

quack quixote

Posted 2010-04-13T09:15:07.503

Reputation: 37 382

2

setenv TITLE "%{\033]0;%n@%m:%~\007%}"
set prompt = "${TITLE}%n@%m:%~%#"

SirDice

Posted 2010-04-13T09:15:07.503

Reputation: 21

what does hardstatus play here? – gcb – 2014-09-16T23:26:11.573

1But will this update the title when running screen – Nathan Fellman – 2011-10-19T16:26:00.070

1This works fine with screen 4.00.02 + tcsh 6.12.00. This assumes you have the screen hardstatus variable set appropriately and termcap/terminfo supports hs|ts|fs|ds. – Darren Hall – 2011-12-24T17:51:11.267

2

This page (which has been linked above) is the solution, however you need to make sure the entry's terminal string lines up with your terminal's id string (ie; what the variable TERM is set to) otherwise it won't have any effect.

Here a summary of what I did, which worked for me after years of not investing enough time to figure this out:

  1. Find out what your terminal's ID string is by running:

    echo $TERM
    

    It probably will say something like 'xterm' or 'linux' or 'rxvt'.

  2. Now add the following line to your .screenrc file (or make one if it doesn't exist) located in your homedir (~/.screenrc):

    termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
    

    The key is to make sure that 'xterm' is your TERM's ID string that you got above. You can do multiple lines for each different term type that you use.

For example I have this as my .screenrc file because I use both xterms and the 'linux' term type when SShing into a box:

termcapinfo linux 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

Hopefully this helps, and is totally shell agnostic. Remember if you run screen as root you'd need to modify ~root/.screenrc in addition to your own ~/.screenrc file like this.

Adam Strohl

Posted 2010-04-13T09:15:07.503

Reputation: 141

1

The instructions here allowed me to update the title in rxvt from bash for me - which is prehaps not of any help but I thought it might be worth investigating (section Informative Statusbar if the link doesn't jump to the right part of the page)

Barry

Posted 2010-04-13T09:15:07.503

Reputation: 11