15

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?

2
  • Have you found a solution that worked for you yet? May 16, 2010 at 3:48
  • No. I tried the suggested answers but they didn't work. That's why I neither accepted them nor upvoted them. May 16, 2010 at 6:17

6 Answers 6

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.

3

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.

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.

2
2
setenv TITLE "%{\033]0;%n@%m:%~\007%}"
set prompt = "${TITLE}%n@%m:%~%#"
3
  • 1
    But will this update the title when running screen Oct 19, 2011 at 16:26
  • 1
    This 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. Dec 24, 2011 at 17:51
  • what does hardstatus play here?
    – gcb
    Sep 16, 2014 at 23:26
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.

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)

You must log in to answer this question.

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