How can I automatically update the title in an xterm running screen?
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:
-
Find out what your terminal's ID string is by running:
echo $TERM
It probably will say something like 'xterm' or 'linux' or 'rxvt'.
-
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:%~%#"
Related videos on Youtube
Comments
-
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 almost 14 yearsNo. I tried the suggested answers but they didn't work. That's why I neither accepted them nor upvoted them.
-
-
Nathan Fellman over 12 yearsBut will this update the title when running
screen
-
Darren Hall over 12 yearsThis works fine with screen 4.00.02 + tcsh 6.12.00. This assumes you have the screen
hardstatus
variable set appropriately and termcap/terminfo supportshs|ts|fs|ds
. -
gcb over 9 yearsthis will not work when inside gnuscreen. at least it is not working for me. thats why im here :)
-
gcb over 9 yearswhat does hardstatus play here?
-
Senthilkumar Manoharan over 9 yearsDoes this help? aperiodic.net/screen/faq#how_can_screen_use_xterm_s_title_bar