corey, para llevar

quick gnu screen tips

I go through phases with the tools I use. My preferences regarding my setup are evolving over time as I grow as a developer. One tool I have sort of rediscovered recently is gnu screen. Doing a lot of pair programming I’ve found that while my pair may know of screen, not all developers are aware of its extensive abilities. I wanted to share some useful functionality with you that you might not know about.

getting a jump-start

Often you want to begin your screen session with a command already running inside rather than the default shell you get. For instance if you wanted to have irb running inside screen on start:

screen irb

You will be presented with a screen session with irb in the first window (0).

existing in a state of detachment

If you want to have screen detach immediately upon starting, then you’ll want to use this option:

screen -dm

Combined with the ability to invoke a command of your choice, this can be very useful when you want to run a program in the background. Being the awesome program that screen is, you can then attach to the window later even if you have disconnected.

sharing is caring

Screen can enable multiple users share a session, allowing collaborative editing.

User foo should start a named screen session. Naming the session makes it easy for the other user to figure out which session to connect.

screen -S shared

Once inside press the following key combos to allow the user bar to join the session:

⌃a:multiuser↩
⌃a:acladd bar↩

Now bar should be able to connect to the session with:

screen -x foo/shared

Often screen is installed without the ability to do this for security. You’ll need to do the following to enable it.

sudo chmod +s /usr/bin/screen
sudo chmod 755 /var/run/screen

This capability of screen combined with Skype or iChat makes for a rather decent remote pair programming setup.

the art of assemblage

You can combine all these options and more into a screenrc file.

screen -S shared 0 irb
screen -S shared 1 vim ~/projects
screen -S shared 2 

If you save the above in a file called ~/.screenrc_projects, you can start the session with:

screen -S projects -c ~/.screenrc_projects

A great idea is to have multiple screenrc files for different task setups that you can alias to invoke. For example, you could create a bash alias that starts screen with a tailored screenrc for a specific project that starts an editor, your test runner, and a interactive interpreter.

handling the zombies without a shotgun

If you don’t want screen to destroy a window when the process in it dies, then you want to add this to your screenrc.

zombie qr

This lets you quit or restart the window after the process terminates.

hungry for more?

Turns out screen is well very well documented in its manual, a rare and beautiful thing. I often get quality information from Stack Overflow, try questions tagged with screen. Also, it doesn’t hurt to ask in #screen.