Gnu screen, how to update dynamically the title of a window?

8

6

I'm trying to improve the aspect of GNU Screen using the screenrc file, I tuned colors, status line, caption and the list of the loaded windows.

The only thing I'm not able to achieve is getting the caption with the current executed command as in the below picture, note the vim caption in the right pane:

enter image description here

What I currently have is this, and what I would like to obtain is having captions (and if possible also hardstatus line) with |0 less| 1 man instead of the current |0 bash| 1 bash.

How can I do this?

Update: Until now the only working solution I found is in this post but it doesn't print what I need...

However that is the only working dynamic title I found until now.

PROMPT_COMMAND='echo -ne "\033k\033\134\033k${HOSTNAME}[`basename ${PWD}`]\033\134"'`

in .bashrc and shelltitle '] |bash' in .screenrc. The title is updated with the hostname and the basename of the working directory, but the prompt is completely wrong (it also has a leading space):

134134fabio@host10:~$

Fabio

Posted 2011-02-10T23:21:37.680

Reputation: 193

checkout the updates on the answer – Torian – 2011-02-12T02:03:14.957

Answers

8

In your .bashrc file set your PROMPT_COMMAND like this:

UPDATE:

case "$TERM" in
    screen*) PROMPT_COMMAND='echo -ne "\033k\033\0134"'
esac

UPDATE: seems to be a bug with echo built into bash. Solution should be either use of /bin/echo or \0134. Try:

$ echo -e "\134 = \0134"
\134 = \
$ /bin/echo -e "\134 = \0134"
\ = \

This is the escape sequence that screen needs to identify which command is being run, and replace the title of your current window.

Then, on your .screenrc file, make sure the following lines exist:

shelltitle '> |something:'
hardstatus alwayslastline

The pattern search|name tells screen to search your end-of-prompt for some string (in this case, '> '). The name part, specifies the default shell name for the window. So, when you have nothing but the shell running, you'll see something like:

$ something:

But when you run top:

$ something:top

Torian

Posted 2011-02-10T23:21:37.680

Reputation: 603

thanks, I'm having it work without the hardstatus ... line in .screenrc, I'm using caption always ..., not a screen guru though. – ryenus – 2014-07-04T01:41:56.947

Note that the shelltitle stuff has to actually be in your .screenrc - nothing will happen if you enter it at the screen command prompt, for some reason. – Greg Bell – 2017-10-01T10:28:56.920

The /bin/echo option isn't portable, the echo binary differs between linux and other unices like BSD and macOS. Using the printf built-in is likely the better solution. – ghoti – 2019-11-05T03:26:20.677

Almost... Your hint for \0134 or /bin/echo works with my above example, so I can have the caption updated with the string between two escape sequences without having my prompt messed. But I can't still make your example working. My prompt under screen is ${debian_chroot:+($debian_chroot)}\u@\h:\w\$ so I'm using shelltitle '$ |bash', is that right? – Fabio – 2011-02-12T14:07:11.127

2

As an alternative, you could use tmux instead of screen. Tmux will automatically set the current window's title.

src

Posted 2011-02-10T23:21:37.680

Reputation: 331