Wednesday, December 5, 2007


Vim has a quickfix feature that allows you to create a list of items to be resolved and easily navigate that list. One easy way to add items to the quickfix list is by using Vim's internal grep feature like so:

:vimgrep /some pattern/ somefile

Once the command is run, the quickfix list is populated with lines matching the pattern in the specified file. From there, you can open the quickfix buffer by using the :cope ex command. Navigating to a line in the quickfix buffer and pressing enter will take you to the specified line in the source file you are editing.

A more advanced use of quickfix is as an assistant in debugging errors across multiple files. A common debugging practice is to insert print statements inside of various conditionals in your source code. If you follow the format "filename:line number:error message", Vim can understand the debugging output of your program and load it into the quickfix buffer. In Ruby (a language I program in often), you would do something like the following:

puts("#{__FILE__}:#{__LINE__}:some error happened")

Place similar statements amongst all files you are interested in tracing, have your program write to a file called "debug.txt", and then open Vim. Issue the following command :cfile debug.txt, and Vim will open the file pertaining to the first error reported at the appropriate line. Again, you can issue a :cope command to navigate the quickfix list and trace through all the errors reported.


Christopher Suter said...

omg this is amazing...thank you!! i had seen stuff about cfile before but i always thought you had to be doing c code with make to use quickfix...killer

Cory said...

Kudos - really quite clever. Like having a stroll through your program's execution.

Anonymous said...

THIS is great!

David Rivers said...

Thanks for the explanations of vimgrep/quickfix/cope!

grillermo said...

Thanks for this post!
I'm using to search in the current document and automatically open it the quickfix like this
:vimgrep /my search/% | cw