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)
- Hop in to TextMate (it’s already running, isn’t it?)
- Select “Bundles>Bundle Editor>Edit Commands” from the menu
- Expand the Ruby bundle, and select the “Run” command
- Click the little ‘double-plus’ at the bottom, which duplicates the current command
- 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!
- Assign a key combo to “Run Last”. I use Command-Ctrl-R.
- Delete the scope in “Run Last” – we want to be able to run it anywhere and everywhere.
- Ignore ‘.tm_last_run_ruby’ in your source control, lest it bug you.
- 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.
My Adam-influenced beaver sense suggests that you put the file under tmp/tools/.
Interesting thought, Austin… but my thinking is that it’s messy enough creating a little dot file, and creating a whole directory that may or may not already exist seems messier. Of course, you’re welcome to tweak it as much as you like if/when you install it :-)
What I’d really like to do is stuff the info in to the TextMate project file, but there doesn’t seem to be a good way of doing that at this point. Maybe the lazyweb will hand me one…
Thanks a lot for this, I’ve wanted this exact functionality for a long time.
Got any suggestions for making this work with the focused unit testing feature in TexMate? I noticed a marked difference in the length of code used to do the regular unit testing and the focused unit testing, so Im sure there are some intricacies with the focused unit testing that needed to be accounted for.
Good job with this, its very handy.
Or the lazyweb will hand you one…
Check out the $TM_PROJECT_FILEPATH variable, and the shellVariables section of that .tmproj file.
Note that you may need to add a shell variable to the project (click on the ‘info’ button of the project drawer with nothing selected) before that section shows up.
Thanks for the post, though … it helped me create a Command called “Run Project” :)
Phil, the project-specific shellVariables are very cool, but while I can see how to programatically read them, I can’t figure out how to set them, which is what I would need. Any ideas? (I’m not keen on doing direct surgery on the project file…)
Thanks!
When you have the project drawer opened, make sure that nothing is selected, then click on the ‘Info’ button (the little I). It’ll bring up a little dialog box allowing you to set project-specific variables that are exported the the environment. Email me if you’re still confused; this blog isn’t on my regular reading list (perhaps it should be, though :))
Oh dang, read that totally wrong! 0→ http://macromates.com/textmate/manual/environment_variables#project_dependent_variables
It says that they’re saved in the *.tmproj file; perhaps you could write to there?
Gah, and I’m still not reading everything. You Don’t want to do direct surgery on the project file (now that I’m looking at things, I don’t believe that will work anyway). I’ll see what, if anything, I can come up with.
Hey Nathaniel,
Just got ahold of Allan Odgaard on irc, and this might help you (I got curious how to do this so I had to find out :))