How I Use Screen

20 Feb 2009

GNU Screen is described a “a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.”

GNU Screen

What this means in plain english is, screen is a whole lot of awesome. Special thanks to @voxdolo for getting me hooked.

Screen essentially lets you run multiple command line based apps, scripts, or prompts from a single terminal and then detach and resume that session whenever you like. This is super useful when you ssh into remote machines and you need to run a script, monitor a log, and just poke around all at the same time. However, it has other applications that motivate me to have it running all the time, instead of just a plain ol’ shell. (I use bash by the way)

First off, screen uses an command character to access it’s commands, by default ctrl-a. However, I type in dvorak layout, mostly on my mbp which has no control key on the right side, making ctrl-a super difficult to press in a hurry. So I changed it to ctrl-h. You can remap any of the bindings in screen however you like.

Using screen in a single context

I know most folks who use screen like to create separate screenrc’s depending on what they need open. That works like this:
I’ve got my default, ‘all the time’ settings in ~/.screenrc and per context settings in ~/.screen/foo. So let’s assume I’m going to be doing some rails work – I’d startup screens with the following command:

  screen -c ~/.screen/rails

That’d fire off a server, autotest, console, and prompt – all ready to go from whatever project dir I was in. The config file for that looks like:

  source ../.screenrc
  
  screen -t svr ./script/server
  screen -t con ./script/console
  screen -t at autotest --rails

Using screen while context switching

Contrary to this, quite a bit of the time I’m flying through one off tasks and switching contexts too much for this approach to be productive. In that case I start screen in my home directory and rely on its ability to bind any command to a key. Using these bindings coupled with a utility that lets you jump around really quickly(I use j) allows you to terminal your way around all your chores. Some of my bindings look like this:

  bind S screen -t svr ./script/server
  bind C screen -t con ./script/console
  bind T screen -t test rake
  bind P screen -t cucumber ./features

Why not just use tabs in Terminal.app or something?

Well, because of many of the other great features like splitting, monitoring, capturing output and others. I’m not going to cover them now, as they’re outside the scope of this article, but I encourage you to give screen a try.