When coding Ruby, I love being able to run a test suite right in TextMate (Command-R), but recently I was wanting something a bit more. Back in my bad old Eclipse days, I used a nice little feature that let you hit Ctrl-F11 to re-launch the last command you’d run in the IDE, regardless of where you currently were. After an intensive cycle of test-code-test-code in TextMate, I was getting tired of having to switch back to my test to re-run it, so I decided to take a page from Eclipse’s book and “Run Last” was born. Lets walk through it.

Create the new command (by ripping off the standard one)

  1. Hop in to TextMate (it’s already running, isn’t it?)
  2. Select “Bundles>Bundle Editor>Edit Commands” from the menu
  3. Expand the Ruby bundle, and select the “Run” command
  4. Click the little ‘double-plus’ at the bottom, which duplicates the current command
  5. Name the new command ‘Run Last’

Make it use our special file

With “Run Last” still selected, paste this in to the text area:



  export RUBYLIB="$TM_BUNDLE_SUPPORT/RubyMate${RUBYLIB:+:$RUBYLIB}"
  export TM_RUBY=$(which "${TM_RUBY:-ruby}")

export TM_FILEPATH=`cat “$TM_PROJECT_DIRECTORY/.tm_last_run_ruby”` cat “$TM_FILEPATH” | “${TM_RUBY}” — “$TM_BUNDLE_SUPPORT/RubyMate/run_script.rb”



What we’re doing is reading a hidden ‘.tm_last_run_ruby’ file to figure out what file to run.

Get the special file written out

Now go back to the “Run” command, and change it to this:



  export RUBYLIB="$TM_BUNDLE_SUPPORT/RubyMate${RUBYLIB:+:$RUBYLIB}"
  export TM_RUBY=$(which "${TM_RUBY:-ruby}")

echo “$TM_FILEPATH” > “$TM_PROJECT_DIRECTORY/.tm_last_run_ruby” “${TM_RUBY}” — “$TM_BUNDLE_SUPPORT/RubyMate/run_script.rb”



This writes out the file about to be run to the special ‘.tm_last_run_ruby’ file.

Enjoy!

  1. Assign a key combo to “Run Last”. I use Command-Ctrl-R.
  2. Delete the scope in “Run Last” – we want to be able to run it anywhere and everywhere.
  3. Ignore ‘.tm_last_run_ruby’ in your source control, lest it bug you.
  4. Run a Ruby file like normal, then go anywhere and re-run it with impunity.

Make it better

This is totally my little hack – if you have ideas for a better way to do it, I’d love to hear them. Also, if there’s a good way to distribute things like this that doesn’t involve all the manual steps, I’d love tips on that, too. And of course, if this helps you, I’d love to hear about it.

Posted by Nathaniel on Feb 8th, 2007

You can still contact Nathaniel at nathaniel@terralien.com