Bill Lovett

A .screenrc for Rails Development

Posted on August 7th, 2006

GNU Screen is a litmus tests for being in-the-know about terminal efficiency. Without it, your desktop tends to quickly get crowded by terminal windows. After you've reached the point of familiarity, though, those individual terminal windows merge into one master window which you can connect to from one computer, then reconnect to from a second without missing a beat.

This can be especially helpful with Ruby on Rails development. You might want to have one terminal for stopping and starting WEBrick. And another for tailing your development log. And another for running rake tasks. And maybe another for a mysql session. And another for your text editor.

A .screenrc file to create terminals for all these activities might look like this:

screen -t emacs
screen -t bash
screen -t server ruby script/server start
screen -t tail -f log/development.log
screen -t mysql
select 0

You could even create per-project .screenrc files like this and easily jump from one type of session to another by using screen -c filename. But there's still a big problem: not all your screens contain shells.

A screen running a command like emacs or tail will die as soon as you kill that process. It's the exact opposite of what you'd expect-- normally you drop back to your shell. In the example above, only the second screen is running a shell. Want to restart WEBrick? You're out of luck. You have to create a new screen manually.

The stuff command makes it possible for a screen to stay alive after the program running inside it has finished. It makes your screen sessions behave more like normal terminal sessions, while preserving the ability to set up your environment effortlessly. The example above would become:

chdir /var/www/projectdir
screen -t emacs
stuff "emacs\015"
screen -t bash
screen -t server
stuff "ruby script/server start \015"
screen -t log
stuff "tail -f log/development.log\015"
screen -t mysql
stuff mysql dbname\015"

To cap things off, you might create a function in your .bashrc which creates or reconnects to an existing session based on a .screenrc file keyword provided as an argument:

function s() {
	 screen -D -R -c ~/.screenrc-$1
}

So doing something like "s myproject" gives you a screen session based on the file ~/.screenrc-myproject.

Back to the index of all blog entries