Screen for Ruby
I’ve been learning Ruby and Rails the past week or so and I really do love it. However, I have lots of terminal tabs open and this was getting quite annoying. One for MySQL, one for the server, one for the Rails console, etc. Then today I ran across a post in the TextMate mailing list from someone with the exact same complaint. The suggestion? The under-used and oft-forgotten screen command. Ah, ha!
I’m a bit familiar with screen, having used it in the pre-tab Terminal days. Ah, the Good Ol’ Days when I had one window for emacs (before TextMate, obviously), one for this server, one for that process, and a utility shell. I never got too crazy with it, but I was able to use it. I knew its capabilities, I just didn’t need anything fancy.
Today I got fancy.
What I really wanted was a screen configuration file for my project. I figure if I can separate the configuration out that way, then I can have different screen sessions running for different Ruby apps. This might come in handy in other places, too, like when I’m compiling on a remote server. It took a while to get just the right config file. Here it is:
escape "^Ff"
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]‘
screen -t ruby 1 tcsh -c ‘cd /Users/dmkash/Develop/Ruby/project; /usr/bin/ruby script/server’
screen -t rails 2 tcsh -c ‘/usr/bin/ruby /Users/dmkash/Develop/Ruby/project/script/console’
screen -t shell 3
I’ve redefined the prefix character sequence from C-a to C-f because I still use emacs quite a little bit. C-a in emacs moves you to the beginning of a line.
Next, I did some fancy stuff with the status line at the bottom of the screen. I cannot take credit for it. I found it here. It is quite nice.
The next three lines open up three windows. The first starts the Ruby server for the project, the second gives me a Rails console for the project and the third is a utility shell. Sweet, huh?
Now, to launch a screen session for my project, I just type screen -c ~/Develop/Ruby/project/.screenrc. To make it even quicker, I added an alias with the project name to my .cshrc file. I gave the session a name using the -S project argument and told screen to reattach to the running session if possible using -D -R. My entire alias line looks like this:
alias project 'screen -S project -D -R -c ~/Develop/Ruby/project/.screenrc'
June 5th, 2008 at 1:35 pm
That’s a really cool approach to eliminating tabs. I personally prefer a few tabs, but I totally forgot about “screen”