Friday, February 13, 2009

Using GNU's screen to set up Rails workspaces

Since I discovered GNU's screen a few months ago, I've been finding more and more uses for it. Recently, I thought it would be helpful to use it to set up and store various development workspaces using sets of custom screenrc-style configuration files. This example may be helpful to Rails developers, but can be modified to launch different development workspaces. If you're a command line warrior and haven't started using screen to manage terminal sessions, I highly recommend checking it out.

For Rails development, I typically like to have a separate window open for controllers, models, views, helpers, mysql shell, server, and application root. I use the application root to perform rake commands and run scripts from the script folder. It took some searching to find out how to change working directory in a screen window, so I'd like to share.

Here's my sample screenrc-style file I use for a site I'm currently developing:

startup_message off
vbell on
defscrollback 10000

chdir /Users/jboukheir/Sites/inovatennis.com
screen 0 bash
title "app root"

chdir /Users/jboukheir/Sites/inovatennis.com/app/models
screen 1 vim -p *.rb
title "models"

chdir /Users/jboukheir/Sites/inovatennis.com/app/controllers
screen 2
vim -p *.rb
title "controllers"

chdir /Users/jboukheir/Sites/inovatennis.com/app/views
screen 3
vim -p *.erb
title "views"

chdir /Users/jboukheir/Sites/inovatennis.com/app/helpers
screen 4
vim -p *.rb
title "helpers"

chdir /Users/jboukheir/Sites/inovatennis.com
screen 5 bash
title "mysql"

chdir /Users/jboukheir/Sites/inovatennis.com
screen 6 bash
title "server"

select 0
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r} (%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m%{W}%c %{g}]'



Let's go through what this simple configuration file does:

startup_message off
vbell on
defscrollback 10000

This turns off the startup message, turns on the visual bell ("Woof!"), and sets the scrollback buffer to 10000 lines.



chdir /Users/jboukheir/Sites/inovatennis.com
screen 0 bash
title "app root"

chdir /Users/jboukheir/Sites/inovatennis.com/app/models
screen 1 vim -p *.rb
title "models"

chdir /Users/jboukheir/Sites/inovatennis.com/app/controllers
screen 2 bash
title "controllers"

chdir /Users/jboukheir/Sites/inovatennis.com/app/views
screen 3 bash
title "views"

chdir /Users/jboukheir/Sites/inovatennis.com/app/helpers
screen 4 bash
title "helpers"

chdir /Users/jboukheir/Sites/inovatennis.com
screen 5 bash
title "mysql"

chdir /Users/jboukheir/Sites/inovatennis.com
screen 6 bash
title "server"

This will open 7 screen windows in the directory specified by screen's chdir command. Note that you need chdir BEFORE the screen directive. The line "screen 0 bash" tells screen to open a new window, number it 0, and then execute bash. Similarly, "screen 1 vim -p *.rb" tells screen to open a new window, number it 1, and then execute vim. You can set the window's initial title with the title command.




select 0
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r} (%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m%{W}%c %{g}]'

These lines tell screen to select window 0 by default, set the status to the bottom of the window, and display this status along with some other useful information.

To launch your new workspace, just type

$ screen -c ~/.screens/inovatennis.com

where ~/.screens/inovatennis.com is the path of the screenrc-style configuration file you saved. For a good screen reference, see SGI's screen reference. I hope someone finds this useful!

2 comments:

  1. say mang ... in the views depending on what version you are running it could be *.erb.html or *.rhtml .... You might have changed this in your rc file by now ....

    ReplyDelete
  2. If you're going to use Vim, check out tpope's rails.vim plugin. Stop running multiple instances and learn how to use its convenient navigation commands.

    ReplyDelete