Understanding GNU Screen's captions

I love screen. I know all the cool kids are using tmux now, but screen keeps it simple and does everything I really need.

One of the things I (possibly mistakenly) want is lots of windows. The problem with having lots of windows is they quickly become hard to keep track of, especially since the default screen configuration doesn't have any guiding information quickly available to the user. Fortunately, you can fix this with a caption that appears at the bottom of your terminal.[1]

screen caption

I do this in my ~/screenrc, which boils down to:

### pass commands to screen for describing windows
shelltitle '$ |sh'

### set caption
caption always '%{= kw}[ %{y}%H%{-} ][ %= %-Lw%{+b M}%n%f* %t%{-}%+LW %= ][ %{r}%l%{-} ][ %{c}%c%{-} ]'

There's a lot going on here, and it took an unfortunately long time to work out...

shelltitle '$ |sh' is all about making it easy to know what's in each window, but it's only possible with this extra scripting in my ~/.bashrc:

# dynamic titles for screen
case $TERM in
  screen*) export PROMPT_COMMAND='echo -n -e "33k33\\"'
esac

This passes the last command to screen to use within my caption to identify what's currently happening in each window. This is a lifesaver for someone who makes the questionable life decision of having at least eight windows open in every screen she launches.

That's the simple part; now, let's break down my complicated caption format. Captions aren't documented well. Lots of people post theirs, but usually without explanation - likely because it's a pain to describe. I started off by mashing up other people's captions, but that didn't get me very far. I completely redid mine last week, and I realized pretty quickly that I'd only make it perfect by understanding how to build it up part by part. Inspired by Jay Sitter's article on hardstatus strings, I present a breakdown of my caption:

### set caption
caption always '%{= kw}[ %{y}%H%{-} ][ %= %-Lw%{+b M}%n%f* %t%{-}%+LW %= ][ %{r}%l%{-} ][ %{c}%c%{-} ]'

%{= kw} clear all text attributes and set to text color to white and background to black
[ plain text to mark sections
%{y} set text color to yellow
%H hostname of the system
%{-} go back to previous text settings (text color to white)
][ plain text to mark sections
%= with the later %= in the caption, pad this section on the left so that the caption spans the entire line
%-Lw list windows before current window, the optional 'L' indicates that these windows show their flags
%{+b M} the current window section starts, make text bold, set text color to magenta
%n current window number
%f flags of the current window
* plain text I use to mark the current window
%t current window title
%{-} go back to previous text settings (text color to white, normal weight)
%+LW list windows after current window, the optional 'L' indicates that these windows show their flags
%= with the earlier %= in the caption, pad this section on the left so that the caption spans the entire line
][ plain text to mark sections
%{r} set text color to red
%l current load of the system
%{-} go back to previous text settings (text color to white)
][ plain text to mark sections
%{c} set text color to cyan
%c 24-hour clock
%{-} go back to previous text settings (text color to white)
] plain text to mark sections

Hopefully this explanation inspires you to customize your screen caption to your heart's content!

Oh, by the way, my dotfiles are now available on GitHub.

[1]You could also fix this with a hardstatus. I don't like this solution as much because hardstatus is for status messages from screen, e.g. to alert you about activity, while caption is more thematically in line with giving the details of the windows within the screen.