Monday, 9 April 2012

Populating the vim error-list from a file

When using vim to compile code it is possible to let it parse the messages from the built tool and create a error-list that can be used to jump to the correct files (and most often the correct line).

When using a tool such as grep or the Windows Powershell Select-Pattern it is possible to obtain output that resembles a certain form:

filename:linenumber: found-string
filename2:linenumber2: found-string

This can be used in vim to create an error-list as well, that can be used to jump between these matches incredibly fast - the only things that need configuration are the grep-command, the makeprg-command and the errorformat 1.

Adjusting grep

To obtain all the necessary information from grep it is necessary to issue the command with the following parameters:

grep -Hn pattern file
-H

Will make grep print the file the match was found in.

-n

Will print the line-number of the match.

Adjusting makeprg

Instead of compiling a program, the makeprg command should simply echo back the current buffer that should be used to create the error-list - this can easily be achieved by simply using the echo command.

Therefore the following command must be used in vim.

set makeprg=cat\ %

When running the command make in vim now it will simply echo the currently active buffer and try to match the pre-defined errorformats against it 2.

Adjusting the errorformat

Running make after adjusting the makeprg-command might already work - if not a new errorformat can be set with the following text:

set errorformat+=%f:%l:%m
%f

Will identify the token that represents the file-name.

%l

Will identify the token that represents the line-number.

%m

Will identify the token that represents the error-message 3.

Due to the += the new errorformat will simply be appended to the already defined ones and not overwrite former lists.


  1. It is also possible to use the vimgrep-command - especially on Windows that seemed to be very slow and using the built-in commands and this method proved much faster.

  2. To see the pre-defined formats the command set errorformat? can be used.

  3. To see more possible tokens that can be used check :h errorformat.