Jump to Errors in Ruby Koans with VIM

I attended a Ruby Koans hack night the other day. Since I already know most of Ruby, I decided to see if I could get VIM’s make quickfix support running on the koans – the program prints a file and line, so at best it will just work and at worst I’ll have to get into writing an output parser.

Quickfix Mode

Quickfix mode (:help quickfix) is a feature of VIM designed for grabbing compiler output and stepping through the errors and warnings. VIM parses it into a list of file, line, message. It can jump you to the file in question, and has facilities to step back and forth through the list. Quickfix mode is also used by vimgrep and the Ack plugin to allow you to step through search results – which is where I spend no small part of my time.

WIth the ack plugin:

:Ack mystery_method

The plugin auto-opens the quickfix window. You can use :copen to do it yourself. Once you’ve got a list of search results (visible or hidden), you can step through the list with :cn[next] and :cp[revious]. I do this so much that I bound C-n to :cn

nnoremap <leader>a :Ack 
nnoremap <C-n> :cn<cr>

Make

The :make command runs the make program, make by default, and puts you into quickfix if there is anything that looks like a file-line pair. The make program is defined by the vim setting makeprg. I don’t do the koans very often, so I just set it at runtime.

" wrong
:set makeprg=rake

Unfortunately, this simple version doesn’t quite work. The koans helpfully provide text coloring, which is great for terminal use, but VIM sucks the control codes into the filename and gets confused when it can’t find the file. I initially hacked the rakefile to remove the offending codes, but with a moment’s reflection I realized that the koans might just have an option to deal with this. A little code inspection turned up the NO_COLOR environment variable.

There is just one more complication: you need a space between an env variable and the command, but spaces separate settings passed to the :set command. So, we’ll have to escape the spaces.

" right
:set makeprg=NO_COLOR=true\ rake

Now just :make and VIM should jump you straight to the first line that needs your attention. The koans only report one error at a time, so you’ll need to :make after every change – this might be a good time for a runtime keybinding.

Posted Sunday, January 6th, 2013 under War Story.

Tags: ,

Comments are closed.