12

I am trying to get screen to set my xterm title. I have this working outside of screen, but screen keeps whatever title was in place when I started it. Here is my .bashrc:

function bash_prompt_command() {
    # How many characters of the $PWD should be kept
    local pwdmaxlen=25
    # Indicate that there has been dir truncation
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    NEW_PWD=${PWD/#$HOME/\~}
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
        NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
        NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
    fi

    export NEW_PWD
}

PROMPT_COMMAND=bash_prompt_command
# Color chart @ http://wiki.archlinux.org/index.php/Color_Bash_Prompt
case "${TERM}" in
    "xterm")
        TITLEBAR='\[\033]0;\u@\h > ${NEW_PWD}\007\]'
        PS1="${TITLEBAR}\[\e[1;32m\][\e[0;36m\]\u\e[1;32m\]@\e[1;33m\]\h\e[1;32m\]] \e[0;37m\]\${NEW_PWD}/ \e[1;32m\]\$ \[\e[0m"
        ;;
    "screen")
        TITLEBAR='\[\033]0;\u@\h > ${NEW_PWD}\007\]'
        ESC='\[\ek\e\\\]'
        PS1="${TITLEBAR}\[\e[1;32m\][\e[0;36m\]\u\e[1;32m\]] \e[0;37m\]\${NEW_PWD}/ \e[1;32m\]\$ ${ESC}\[\e[0m"
        ;;
    *)
        PS1="\[\e[1;32m\][\e[0;36m\]\u\e[1;32m\]@\e[1;33m\]\h\e[1;32m\]] \e[0;37m\]\${NEW_PWD}/ \e[1;32m\]\$ \[\e[0m"
    ;;
esac

And here is my .screenrc:

hardstatus alwayslastline
hardstatus string '%{= kg}[%{Y}%H%{g}][%= %{= kw}%?%-Lw%?%{=b kR}(%{W}%n-%t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}][%{Y}%l%{g}]%{g}[%{B}%m.%d.%Y %{G}%c%{g}]'

termcapinfo xterm|xterms|xs|rxvt ti@:te@
termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

altscreen on
shelltitle '$ |bash'

What am I doing incorrectly?

Update (19 August 2010): The problem is that you cannot update the terminal's title from within screen when you set alwayslastline. So my solution was to just give up and settle for a predetermined, useful, title for my screen sessions. My updated .bashrc and .screenrc can be found at http://bitbucket.org/jsumners/rcfiles/src.

5 Answers 5

4

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.

7
  • This is not working. I don't understand why. Jul 6, 2009 at 15:06
  • I have determined that setting hardstatus alwayslastline as I do for screen prevents screen from setting the xterm title. Now if I can just figure out how to do both at the same time. Jul 6, 2009 at 19:47
  • Is it because your hardstatus has %t (for title) in it?
    – crb
    Jul 6, 2009 at 20:10
  • Nope. Just removed that and get the same result. I believe I am going to have to set an alias for screen that sets the window title and then launches the program. And then just give up on dynamic terminal titles while using screen. :( Jul 7, 2009 at 12:17
  • 1
    bugs.launchpad.net/byobu/+bug/338722/comments/10 says it's a limitation in screen
    – agnul
    Jul 9, 2009 at 14:38
7

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
0
1

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

1

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.

0

Last 2 weeks I have searched many forums and I haven't found the answer for the question:

How to get all command output to Putty title?

Needed it for other programs to know when some jobs on a server is done and is it done right or wrong. Plink stdout and stdin wasn't working, I used many tweaks with wait delays and for some commands that worked for others not. XSEL and XCLIP couldn't be installed on that server.

So here is the solution:

  1. Get command output in a file.
  2. Echo that file to title.

On putty client and suse server it looks like this:

ls /home | grep domagaja > logutoit.txt 
echo -e "\033]0\\;$(cat logutoit.txt)\\007\\c"

Hopefully someone will be able to use this. It won't work for all server types and putty client settings, of course, but the idea should work well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.