September 5, 2011

HOWTO: A Status Line in Screen

This is a quickie. This summer, I taught CMSC 330 at UMD (Organization of Programming Languages). After a day of in-class demos, a student sent me a heads-up about GNU screen, which I'd admittedly never heard of before. GNU screen is a "screen multiplexer" for the terminal.



We've probably all had the need for "screen multiplexing" before. When I'm teaching or doing anything long-term, my preferred method is to connect to my home server here at btr3.com, so that I have complete control over my development and editing environment. This is probably only possible because of how much I enjoy using "vi" as a text editor. X-forwarding from home would be painfully slow.

So where does a screen multiplexer come in? It's pretty common when actively developing some software (for educational purposes or otherwise) to edit in one "window" and debug in another. You could even add a third window for compiling or a fourth window for documentation. Regardless, working with only one screen can be a pain. However, when connecting over a command-line interface (CLI), having multiple terminals open can also be a pain. You'd have to launch and maintain separate windows for each Terminal, and multiple client connections with the server. This setup is necessarily bad, but it's cumbersome, depending on your SSH client. Even in a tabbed client, keeping the tabs straight while working can be annoying.

Enter GNU Screen: http://www.gnu.org/software/screen/

The basic idea of Screen is that it allows a single CLI to loop over several "screens" of info. The screens act as independent terminals, but only one SSH connection is necessary to your server. Even more crucial than the existence of the program was this tidbit about adding a status line, which makes it possible to see screen "ids", making switching between specific screens possible.

All you have to do is install GNU screen on your Linux system. Then create a .screenrc file in your home directory and add this line:

hardstatus alwayslastline "%{-b gk}%-w%{+b kg}%50>%n %t%{-b gk}%+w%<"

Then, when you type "screen", you'll see a line at the bottom of the screen with screen ids. Even though they may all say the initial command (e.g. "0 bash", "1 bash", etc.), this helps to keep them straight. Anyone have other Screen tips? I'm impressed so far.

Also, credit for pointing me to this goes to a student in my 330 class this past summer. At his request, I'll add his name for some credit here, for whatever that might be worth. By default, though, I'm obligated to anonymity.

1 comment:

  1. IMHO, best feature of screen is keeping session. You can start session, lost your connection (because of any network problem or reconnecting from other host) and continue working...

    ReplyDelete