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.