Gnu Screen ; the .screenrc

I wrote this Gnu Screen guide in December '08 with the previous incarnation of linux.com in mind. January 2009 however, linux.com started on its new and exciting path as a community site and the article just sat on my machine, waiting. So I decided to publish it on my blog in a four part series over the following month. Maybe someone can use it ;)

Have you ever had to run a time consuming process (say mkfs or compiling something) over a shoddy ssh connection? Or cleanly run several commands in parallel without having several open connections to that server? Or maybe you were programming and wanted a mysql prompt, a bash prompt and several text editors open? How about that ssh tunnel you need open and dont want to close by accident? And did you ever need to perform the same task on several different machines? Or connect to a serial terminal? Or maybe you just want a log file of what you re doing..
These are day to day situations any linux user will run into, that can be quite frustrating. That is, before you discover Gnu screen!

"Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells." Or at least, thats what the site calls it. Sounds easy enough, but there are an amazing amount of different uses for this incredible piece of software. The above list is some of the uses I could readily think about - some of the ways I regularly use screen, but like any decent linux tool - the sky really is the limit! I'll be looking into different issues, so lets jump into some of the practical solutions for those problems.

The Fun Stuff - the .screenrc
Trough these chapters, we have seen a huge amount of options and tweaks for screen and some of these can become a bit too much to manage. If you need to start the same screen session each day, with a specific window and region configuration, running those same commands each day. It can become quite the bore to set that up day after day & because of that; screen provides the option to configure these settings and more in the .screenrc configuration file! The .screenrc is a clear text file where you can really insert anything you might think you need. To at least give you a head start, I'll run trough the .screenrc file i built, based upon my colleague, Guy Van Sanden's .screenrc file.

First off, we start with some general options to make our screen life easier..

autodetach on
startup_message off
defscrollback 10000
vbell on

These are all just basic options for your convenience. The autodetach option guarantees that screen acts like we want it to. Continuing its work in a detached state. This is actually the default option, but it cant hurt to set it in the config to make sure. The startup_message option disables or enables the info message you normally get when starting screen. Read this once! Screen is worth your time. And then disable it to make your screen sessions more worth while. The defscrollback option sets how many lines need to be kept for the copy buffer. 10000 sounds like a nice number and definitely more than you ll ever need. But I'm playing on the safe side in my config ;) The vbell option sets the visual bell option. For those that enjoy the audible "bell" sound.. If you dont, you can further customise this using "vbell_msg" and "vbellwait"

A second possibility to touch here are keybindings. Screen provides an easy way to bind commands to custom keys. There are a few keybindings configured in the file I inherited from Guy, but none of them are really of much use to me. If any, the following remove some of the key bindings that could be troublesome.

bind k
bind ^k
bind .
bind ^\
bind \\

These keybindings remove the standard bindings for closing and killing your screen sessions and that can indeed be dangerous or bothersome. Nobody wants to close their sessions by accident! The syntax is really simple.

bind key command

And don't let the fact that I have found no keybindings worth configuring put you off. You should do mad bindings & post them all in the comments!!

We had an extensive talk about screen windows, how to use them and how to put them to good use.. As I mentioned in closing at the time, you 'll probably quickly get bored of opening these windows over and over again. This is why the config file has the following option

screen -t name WindowNr

This opens a screen window with the parameters you gave. You 'll probably want to open several windows to really make use of the possibilities.

screen -t bash  0
screen -t foo 1
screen -t bar 2

Next on the menu is some minor customisation to the messages screen returns. As an example we configure the power detach message..

pow_detach_msg "Screen session of \$LOGNAME \$:cr:\$:nl:ended."

Screen allows for a lot more of configuration on this front too, so again - you should go trough the screen manpages to fully make use of these options.
And finally, to put all the previous options together, we re configuring the hardstatus line. This is a status line where you can really show a lot of usefull information. My personal screenrc file uses the following hardstatus line.

hardstatus alwayslastline "%{rw}%H%{wk}|%?%-Lw%?%{wb}%n*%f %t%?(%u)%?%{wk}%?%+Lw%?"

The line shows the hostname, the available windows, if these windows are being logged or not and it shows the window that is currently marked as being active. So lets take a closer look. The "alwayslastline" part tells screen to have his visible at all times on the last line of the screen screen. %{rw} sets the text color to red and background color to white. %H shows the hostname where screen resides. This will come in handy when you're administrator for different boxes, making sure you dont format the wrong server. %{wk} sets white text with a black background. The | just puts a | in the line. The %? part tells screen to only show the following parameters if they re non empty. We use this a lot. The %-Lw flag shows the current window. After this, we show a lot of relevant information %{wb} sets the color to white text with a blue background. %n prints the window number.* Shows a star. Sometimes a star is just a star ;) %f flags the window when relevant. %t shows the window title. (%u) shows all the other users in this window between brackets. The next new tag is %+Lw, which provides all the screen window information except the current one.

And all the above provides us with the following, extremely usefull, statusline.

Hostname|0-$(L) window0  1*$ bash  2$ bash

Anyone frequently using screen should consider customizing their .screenrc file for more ease of use.

The Ubuntu Standard ScreenRC stuff
I had initially intended to give an overview of it all here, but that was before byobu. Byobu is an extremely advanced configuration package for screen; taking a lot of the configuration out of your hands and honestly a wonderful addition to screen. Its installed by default on Ubuntu; check it out at help.ubuntu.com: Byobu

More about me

Site QR Code

Use this code at your own peril; I am not responsible for anything that happens to you or your devices. You re a big boy or girl, please do some research before using it if you have any concerns!