February 22, 2008

Posted by John

Tagged terminal and workflow

Older: Git and GitHub

Newer: How to represent HTML/XML in YAML

Handy iTerm Shortcut

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.

6 Comments

  1. 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.

  2. I am blessed by your osascript skills. I shall use this every day.

  3. @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.

  4. David Dai David Dai

    Feb 26, 2008

    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

  5. David Dai David Dai

    Feb 26, 2008

    Forgot to mention once you got your yaml configuration, the process of opening up a project becomes:

    iterm $proj_trunk

    enjoy :)

  6. cyn0n rautha cyn0n rautha

    Feb 28, 2008

    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 ;)

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.