Handy iTerm Shortcut
February 22nd, 2008
So each time I go to code in a rails app I open up three tabs: one for script/server, one for autotest and one for general commands like script/generate and such. Let’s say I want to open up tinye, a project I’m working on at work. I have to do the following:
cd sites/nd/tinye.git script/server # new tab cd sites/nd/tinye.git a #that is my shortcut for autotest -rails # new tab cd sites/nd/tinye.git
I got a bit tired of this, so based on some scripts I saw around the interwebs, I put together the one below:
#!/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
# how to make new terminal (make new terminal)
osascript <<-eof
tell application "iTerm"
tell the last terminal
activate current session
tell the last session
set name to "server"
write text "cd \"$PROJECT_DIR\""
write text "ss"
end tell
launch session "Default Session"
tell the last session
set name to "autotest"
write text "cd \"$PROJECT_DIR\""
write text "a"
end tell
launch session "Default Session"
tell the last session
set name to "app"
write text "cd \"$PROJECT_DIR\""
write text "open http://localhost:3000/"
end tell
end tell
end tell
eof
I put this in /bin, named iterm.sh, and made sure it was executable (chmod u+x ~/bin/iterm.sh). Then I threw the following in as aliases (/.bash_profile for me):
alias r='iterm.sh $1' alias ss='script/server' alias sc='script/console' alias a='autotest -rails'
This allows me to open the same project I talked about earlier by simply typing:
r sites/nd/tinye.git
Instantly, I get three tabs in iTerm (server, autotest and blank) and an open browser window on localhost:3000. Thought I would share it here to spark other ideas like this. Also, I’m curious if others have similar things (or cooler things). Below is quick and dirty screencast that shows it in action.

February 22nd, 2008 at 07:07 PM
Since you asked for similar things, I’ll mention that I ended up replacing a similar iTerm tabbed setup + applescript with GNU Screen. Any blog post like this about tabbed terminals invariably has somebody mention screen, and I finally gave in and checked it out.
It’s not going to change your life, but I find it handy, and it doesn’t lack anything my iTerm solution gave me. After using it a couple of weeks, I’ve enjoyed the ability to easily search back through the buffer, split windows, and especially the ability to detach and reattach screen sessions.
For grins I pastie’d my (simple) configs: http://pastie.caboo.se/156152
It achieves the same goals, but it’s nice to know that if I switch back to Linux some day, or am SSH’d into a remote server, screen will work the same.
February 24th, 2008 at 09:01 PM
I am blessed by your osascript skills. I shall use this every day.
February 25th, 2008 at 10:00 PM
@Dr Nic – Ha. Well, you should be more impressed by my “copy and hack code to do what I want skills” as that is pretty much what I did.
February 26th, 2008 at 07:29 AM
Howdy, I’m glad to see I’m not the only one that likes to DRY up our daily morning routine. Here’s a Ruby script that I cooked up a few months back to do my own iterm tab forking. It reads in a yaml file of your choice and will execute the commands that you specify in order. And it’s in Ruby. :)
http://pastie.caboo.se/157550
February 26th, 2008 at 07:34 AM
Forgot to mention once you got your yaml configuration, the process of opening up a project becomes:
iterm $proj_trunk
enjoy :)
February 28th, 2008 at 05:03 PM
3 cheers for screen… unlike the other guy before me I’d highly recommend you switch to screen—thnx for the other bits of advice on your site however ;)