Today, I Learned...

by Justin Campbell

< All

Annotate Ruby code in Vim with xmpfilter

vim-ruby-xmpfilter screenshot

rcodetools contains a utility called xmpfilter, which annotates Ruby code with intermediate results. This is great for testing ideas, explaining them to someone else, giving a live-coding talk, etc.

vim-ruby-xmpfilter provides Vim functions for xmpfilter.

First, we need to install rcodetools:

$ gem install rcodetools
Successfully installed rcodetools-0.8.5.0
1 gem installed

Then, add vim-ruby-xmpfilter to your Vim installation. My preferred way is with Vundle:

Bundle 't9md/vim-ruby-xmpfilter'
$ vim +BundleInstall +qall
$

Also, add mappings for keystrokes to your Vim config. I just stuck with F4/F5 suggested by the vim-ruby-xmpfilter README:

" xmpfilter
map <F4> <Plug>(xmpfilter-mark)
map <F5> <Plug>(xmpfilter-run)

Now we can open any Ruby file and start playing:

1 + 2

Press F4 to annotate the line:

1 + 2 # =>

And then F5 to update all annotations in the file:

1 + 2 # => 3
July 1st, 2013