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

13,153

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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

Related videos on Youtube

Nathan Fellman
Author by

Nathan Fellman

SOreadytohelp

Updated on September 17, 2022

Comments

  • Nathan Fellman
    Nathan Fellman over 1 year

    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?

    • Admin
      Admin almost 14 years
      No. I tried the suggested answers but they didn't work. That's why I neither accepted them nor upvoted them.
  • Nathan Fellman
    Nathan Fellman over 12 years
    But will this update the title when running screen
  • Darren Hall
    Darren Hall over 12 years
    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.
  • gcb
    gcb over 9 years
    this will not work when inside gnuscreen. at least it is not working for me. thats why im here :)
  • gcb
    gcb over 9 years
    what does hardstatus play here?
  • Senthilkumar Manoharan
    Senthilkumar Manoharan over 9 years