I'm about to show you how I roll with iTerm and Rails application development. It may seem a little screwy to a proficient TextMate users, but hear me out. I know my way around Vim. That makes me very productive with it. My problem with TextMate, is that it isn't totally about getting away from the mouse, clicking, and pressing arrow keys. Don't get me wrong; they get a *lot* right.
"Damn, I though Rob was going to blog about something other then his aversion to non-home row hand positions!"
My whole Mac life changed when iTerm got tabs. It really got me to a place where I didn't miss Linux as bad. But still, opening tabs, logging into servers, changing directories, and starting services, still takes time. When you bounce between projects as often as I do, it becomes a real problem. I had to figure out a way to script some of this.
So, here's what that quest produced. It's a shell script, that runs AppleScript (what a goofy language!) and gets my iTerm window all set up to jump into a project after having been away for a bit. In other words, it opens up tabs and executes various commands in each tab, all following a single press of the "return" key. Check it out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#!/bin/sh
if [[ $# == 0 ]]; then
PROJECT_DIR=$PWD
elif [[ $# == 1 && -d "$1" ]]; then
PROJECT_DIR="$@"
else
print "usage: iterm.sh [rails project directory]"
return 1
fi
osascript <<-eof
tell application "iTerm"
make new terminal
tell the last terminal
activate current session
launch session "Default Session"
tell the last session
set name to "app"
write text "cd \"$PROJECT_DIR\"/app"
write text "clear; ls"
end tell
launch session "Default Session"
tell the last session
set name to "app"
write text "cd \"$PROJECT_DIR\"/app"
write text "clear; ls"
end tell
launch session "Default Session"
tell the last session
set name to "server"
write text "cd \"$PROJECT_DIR\""
write text "./script/server"
end tell
launch session "Default Session"
tell the last session
set name to "console"
write text "cd \"$PROJECT_DIR\""
write text "./script/console"
end tell
launch session "Default Session"
tell the last session
set name to "yell"
write text "cd \"$PROJECT_DIR\""
write text "cd log"
write text "tail -f yell.log"
end tell
end tell
end tell
eof |
To use, save to an executable file and run from the same directory as your Rails project. Pass the project directory as an argument to the script.
$ iterm tupleshop.blog
then watch what happens:

I use the first two tabs as my primary work areas. I usually have the Vim screen split once or twice. Any more editor's open and I usually loose track of where things are. The functions of the remaining tabs should be evident. The only strange one is named "yell." That's where I tail my stupid simple log file (that I blogged about once) called yell.log.
So, if you find this solution, or the problem I'm trying to solve, interesting, please share your ideas.
(I'd love to meet a TextMate pro and swap for Vim training sessions. Although, I know from experience that getting fast is not something you do in one sitting.)





Cool! I was just thinking earlier how often I set up three tabs in iTerm: tail -f log/development.log, script/console and one for everything else…
Many thank for this hint. i will use it from now (with some modification for my own purpose
Not a bad a idea, I usually manually open everything which is quite tedious. My needs are a little different from yours however, I’m on Ubuntu and use GVim with the rails.vim plugin. So I switched up the script for my needs.
!/bin/bash
if [[ $# == 0 ]]; then PROJECT_DIR=$PWD elif [[ $# == 1 && -d ”$1” ]]; then PROJECT_DIR=”$@” else print “usage: iterm.sh [rails project directory]” return 1 fi
cd $PROJECT_DIR
gvim
gnome-terminal \ —tab -t “app” \ —tab -t “server” -e “sh -x -c ’./script/server’” \ —tab -t “console” -e “sh -x -c ’./script/console’” \ —tab -t “log” -e “sh -x -c ‘tail -f log/development.log’”
You can grab rails.vim from here,
http://rails.vim.tpope.net/
Thanks for the post.
That’s a great time-saver, I’ve also added another tab that runs autotest.
Is there any way to detect if the current tab has completed it’s commands before opening the next as I would like to make the script do an “svn up”, “rake db:migrate”, “rake db:test:prepare” etc. but wait for it to complete before starting servers/running tests etc.
Or, you could just use Linux. Someone forcing you to do otherwise?
@”Author can’t be blank”: No one is forcing me to use a Mac at this point.There are many things about OS X that I would have to live without with Linux(a Linux laptop, specifically). At ILM my primary desktop developmentenvironment was Red Hat 7.3. When I came on board at O’Reilly I was asked if Iwanted a Linux laptop or a Mac. My manager suggested that development might besmoother because almost all technical people at O’Reilly used Macs. Because ofthis, I chose to develop primarily on a Mac but my current work station alsoincludes an Ubuntu desktop which is an extension of my Mac desktop via <ahref>Synergy.
I have been thinking about going back to Linux recently. However, experiencetells me that instead of blogging about minor points like scripting terminalwindow behavior, I would instead be confronted with show stopping issues suchas basic power management or hibernation (suspension). I still think the rewards of running Linux would make up for any troubles I may encounter. We’llsee what I decide to do.
Thanks for your comment.
Thank you for the script. I modified it to AppleScript only since I needed a script that would open a specific RoR app that I maintain. I also have it opening the project in Textmate. But I went to school off of your stuff.
Good work!
My Script:
tell application “TextMate” open “Macintosh HD:Users:...:helpdesk_project:trunk:helpdesk.tmproj” end tell
tell application “iTerm” terminate the first session of the first terminal make new terminal tell the last terminal launch session “Default Session” tell the last session set name to “Helpdesk Project” write text “cd /Users/.../helpdesk_project/trunk/app” write text “clear;ls” end tell end tell
tell application “TextMate” activate end tell
The “yell” log is very handy. I tried to leave a comment in that blog entry, but couldn’t. Anyway, thanks for writing about it. It makes it much easier to get to the bottom of some temporary issue.