Running Ruby tests from Vim
There’s been something of a Vim renaissance among the Reevoo developers of late. It was driven initially by necessity: a couple of developers live far away and spend a couple of days a week working from home, but wanted to be able to continue doing pair programming despite the physical distance.
Latency means that GUI solutions like VNC suck, so we’ve ended up using a combination of GNU Screen and Vim. Vim’s ideal for low-bandwidth and high-latency connections, and Screen lets two people use the same terminal.
By comparison, all the developer machines in the office are Macs, and most of us use TextMate as an editor. It’s a good product: it’s easy to get started with (unlike Vim!), but it’s also highly customisable—and it comes supplied with a number of language support bundles, doing everything from lining up equals signs to asking Subversion who’s responsible for writing a particular line of code. The most-used combinations from the Ruby bundle are probably Command-R (run the current file) and Command-Shift-R (run the test under the cursor), because they really speed up the test-driven development cycle.
I’ve been a Vim user for a long time, but using TextMate at work has made me realise just how much I was missing! Fortunately, Vim is just as customisable as TextMate. It just doesn’t do as much out of the box.
My colleague James wrote about his Vim learnings yesterday, and commented:
Ideally I’d like to be able to do the equivalent of “run focussed test” in TextMate, which should be quite straightforward, but that’ll have to wait for another day.
Here’s my effort at solving that. In my .vimrc
,
I’ve added a couple of key mappings that only work in command mode
in ruby test files:
" Run Ruby unit tests with gT (for all) or gt (only test under " cursor) in command mode augroup RubyTests au! autocmd BufRead,BufNewFile *_test.rb,test_*.rb \ :nmap gt V:<C-U>!$HOME/.vim/bin/ruby-run-focused-unit-test \ % <C-R>=line("'<")<CR>p <CR>| \ :nmap gT :<C-U>!ruby %<CR> augroup END
gT
should be fairly obvious, but gt
relies on
a small Ruby script I’ve written and stored in my
~/.vim/bin/
directory.
Incidentally, I keep my configuration files in Subversion, so have a browse if you’re interested, or even check the whole lot out:
svn co http://paulbattley.googlecode.com/svn/config