3

In bash, to display the name of the current program in the GNU Screen hardstatus line takes only two configuration lines. First, tell screen what the end of your prompt normally looks like, and supply a default title for a window when you are sitting at in the shell:

shelltitle "$ |bash"

Next, place this escape sequence in the PS1 variable, before the characters that normally terminate the prompt '$ ' in this case: \033k\033\\

This technique works, to a point. The hardstatus window title is updated to the name of the currently running program, and then switches back to the default title shortly after execution is finished. One major problem, however, is that this escape string is not escaped itself, causing line-wrapping problems with commands longer than the initial line.

This was annoying, so I set out looking for a solution. Turns out, simply escaping the previous escape sequence corrects line wrapping: \[\033k\]\[\033\\\]

Great! My hardstatus window title still updates to the name of the currently running program, and now my longer commands wrap to the second line correctly. However, with this new escape sequence in my PS1, screen updates the window title to the actual command I am typing, not simply the name of the current program once it is executed.

I am wondering, has anyone gotten this working correctly - i.e. line wrapping and proper updating of the hardstatus window title?

4
  • I'm not sure I understand what you mean by "screen updates the window title to the actual command I am typing, not simply the name of the current program once it is executed". If you do, for example, echo test; less somefile it will be echo that's displayed as the window title rather than less. Is it that you want less displayed instead? Commented Nov 15, 2010 at 19:14
  • I've tried to fix the formatting of the backslash-heavy sequences, please make sure I've got it right. Note that the preview is a little buggy in this case; backquotes work better than <pre>. Like Dennis, I don't understand what you're trying to achieve exactly, could you give us an example? Commented Nov 15, 2010 at 21:32
  • @Gilles: You have the backslashes correct. Commented Nov 15, 2010 at 23:07
  • for example: if I type execute 'less somefile', the gnu screen window title in the hardstatus will read 'less'. this is correct. However, if I am typing out a long command i.e. 'cat path/to/some/file >> /path/to/some/new_file && chmod 755 /path/to/some/new_file', while I am typing the command the gnu screen hardstatus window title will contain the while command line, not simply 'cat' or 'chmod'. This didn't happen using the unescaped sequence, and although it's not a major issue I'm hoping for a way to remedy it for completeness' sake. Commented Nov 21, 2010 at 18:00

2 Answers 2

4

I don't think screen is the right place to update that copy. It's really easy to do from your shell configs. You can define a preexec() function in your shellrc file that sends off details of what it is about to run before it does it. You can print from there to tell screen to change the current window title. I'm using zsh, not bash, so your mileage will vary, but I know the same things is possible there too. For reference in my zshrc file I have something like this:

# If running in screen, set the window title to the command about to be executed
preexec() {
    [ -n "$WINDOW" ] && print -Pn "^[k$2^[\\"
}

# If in screen, blank the window title when displaying the prompt
premd() {
    [ -n "$WINDOW" ] && print -Pn "^[k ^[\\"
}

In reality I do some cleanup of the command before displaying it as the title. For example you could use print -Pn "^[k`echo $2|perl -pne 's!\s.*/! !g'|cut -c1-16`^[\\" in the preexec() above to show much cleaner information about what is about to be run. This strips off the paths to just the last component and a does a few ninja tricks.

1

Working instructions for Bash are below. I assume you work under root user, therefore # is used in shelltitle. For normal users use $

.screenrc

hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
shelltitle '# |bash'

.bashrc

PS1='\u@\h [\w] \$ '

if echo $TERM | grep ^screen -q
then
    PS1='\[\033k\033\\\]'$PS1
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .