Set xterm (PuTTY) window title when using screen?
Solution 1:
The escape codes are different inside screen.
This outside of screen:
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
is equivalent to this inside screen:
PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME}: ${PWD}\033\\"'
Changing your case ${TERM}="screen" titlebar to
TITLEBAR='\[\033_\u@\h > ${NEW_PWD}\033\\\]'
will solve your problem.
Solution 2:
Actually, there is a way to send escape sequences directly to the xterm, passing through screen. It was not easy to find, but from the screen code (src/ansi.c) I think its been there since 2005. I have it working in xterm, Gnome Terminal, and putty. I found a note in mintty source indicating it does not work there simply because they don't handle DCS (unless it was fixed recently).
From the documentation for screen below (at http://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html )
ESC P (A) Device Control String
Outputs a string directly to the host terminal without interpretation.
The ANSI DCS (Device Control String) is an escape code that is used to send directly to a terminal (I think that was its original purpose from many years ago). Such a string is terminated with an ST escape code (String Terminator).
Wrap the string to set the terminal's window title inside a DCS..ST block, and it goes through screen and updates correctly, even with hardstatus alwayslastline.
DCS = \033P, ST = \033\
Example - to update the window title with the current working directory, use
NEW_TITLE="\033]0;${PWD/$HOME/~}\007"
echo -ne "\033P${NEW_TITLE}\033\\"
Better late than never! Hope this helps someone.
Just in case I'm wrong about the screen source changes: I am running the latest from screen-session git, which in turn is using almost the latest from screen git (with some changes specific to screen-session). But the log messages are dated from 2005 screen (you can see the changes made to that commit regarding DCS handling if you clone the screen git repo and use the command below).
git clone git://git.savannah.gnu.org/screen.git
git difftool e6618a14^! src/ansi.c
Solution 3:
crb is kind of right, but
a) the escape code he uses do not work for me. It should be \033k
instead of \033_
. See screen (1)
under "TITLES (naming windows)"
. So it should be:
PROMPT_COMMAND='echo -ne "\033k${USER}@${HOSTNAME}: ${PWD}\033\\"'
b) this kind of escaping is only used for setting the title of the screen-window. One instance of screen can have multiple windows, each of them has a name, that is what is displayed in hardstatus
and caption
with the t
escape sequence.
c) the normal bash escaping works if you set the right terminfo for xterm in .screenrc
:
termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
(this also works on my machine
for putty)
So in order to set the right title for both the screen window and the putty/xterm title, I use someting like:
XTERM_TITLE="${USER}@${HOSTNAME}: ${PWD}"
SCREEN_TITLE="${PWD}"
PROMPT_COMMAND='echo -ne "\033]0;$XTERM_TITLE\007\033k$SCREEN_TITLE\033\\"'
for reference: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x395.html
Solution 4:
I'm not going to answer the question completely, but let me suggest the way of solving the issue. Many window managers support EWMH specification. There is also a command line tool wmctrl
which is able to interact with them. Using this tool, one may easily change active window's title with the following command:
wmctrl -r :ACTIVE: -T "FooTitle"
It is also possible to change window icon, layout and other properties.The tool probably will not work for PuTTY since Windows doesn't support EWMH, but most of the Linux systems should accept it.
With this method it doesn't matter whether you use screen
or not.