gypsydave5

The blog of David Wickes, software developer

Evaluating Ruby in Vim

I was watching Avdi Grimm’s Ruby Tapas videos - well, trying to watch them. I got stuck when I saw him do something in Vim Emacs that I’d not seen before. Something magical.

On screen he had an expression - something like this:

p = Point.new(2,3)

In one keypress it became:

p = Point.new(2,3) # =>

And then quick as a flash:

p = Point.new(3,5) # => #<Point:0x000000038862d0 @x=3 @y=5>

I immediately stopped paying attention to the excellent video about constructors and started to look up what was going on. So here we go down the Vim rabbit hole again…

The magic is performed by either rcodetools’ xmpfilter tool or seeing_is_believing, hooked up to Vim via vim-ruby-xmpfilter, (which works for both) or vim-seeing-is-believing. Take your pick as I can’t really see the difference at the moment.

I’ve got xmpfilter set up with the following in my .vimrc

autocmd FileType ruby nmap <buffer> <F4> <Plug>(xmpfilter-mark)
autocmd FileType ruby xmap <buffer> <F4> <Plug>(xmpfilter-mark)
autocmd FileType ruby imap <buffer> <F4> <Plug>(xmpfilter-mark)

autocmd FileType ruby nmap <buffer> <F5> <Plug>(xmpfilter-run)
autocmd FileType ruby xmap <buffer> <F5> <Plug>(xmpfilter-run)
autocmd FileType ruby imap <buffer> <F5> <Plug>(xmpfilter-run)

Now you too can evaluate Ruby code on the fly in Vim. And I can get back to watching more of Avdi.