Vim can parse compilation errors when it is passed as an error file using the argument -q. It can also parse the errors produced by running :make. In both these cases, the file path, line and column number of errors can be parsed and traversed using :cnext or displayed in the QuickFix window.
The errorformat is the list of error formats that Vim uses to parse and recognize errors from input text. The default error format in Vim can be seen by trying the command :set errorformat?. The items in the list of error formats are separated by a comma. When separated, these are the components of the list:
This default error format is set in Vim as DFLT_EFM based on the platform as seen in the source code here. Other error formats can be set at runtime based on the compiler flag. The different options for compiler and their corresponding error formats can be seen listed in the files in the directory /usr/share/vim/vim74/compiler. In fact, the default error format string set on Linux is the same as that in /usr/share/vim/vim74/compiler/gcc.vim
For more on error format see :help errorformat in Vim.
The Quickfix feature of Vim can be used to view errors or warnings produced by a compilation in a window and jump to those locations in source files. The Quickfix window has to be manually opened using :copen. Instead, I like the Quickfix window to be automatically opened after running a :make.
This can be done by adding these line to .vimrc:
" Open Quickfix window automatically after running :make
autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost l* nested lwindow
The Quickfix feature of Vim is great if you work regularly with source code of languages which need to be compiled, like C or C++. If you are coming from Visual Studio or Eclipse CDT, the Quickfix feature basically gives an IDE-like feel of the edit-compile-fix cycle. (The only missing piece is a visual debugger inside Vim.) For example, you can view the errors and warnings produced on compiling your code in the Quickfix window, jump to the location of the error or warning, fix them and compile the code again. All from within Vim! 🙂
Here are some pointers on how I use Quickfix:
Quickfix is a plugin that ships with Vim. So, there is nothing to install!
If you have a compile command, then redirect its output to a file. Use the -q option of Vim to open this file and you can navigate the errors from there (as described later below). For example:
$ g++ main.cpp 2> errors.txt
$ vim -q errors.txt
If you are using CMake or Make, then life is much easier for you. Open Vim in build directory and invoke make from within it using the command :make. This runs Make and fills up the Quickfix buffer with the error output.
To open the Quickfix window, use the command :copen. In the screenshot above, the Quickfix window is visible at the bottom.
Navigate the Quickfix window as usual and press Enter on any error or warning line. That file, which may not even be in the same directory, is opened in the top window and the cursor is placed at the line and column of the error. Now, is that not like an IDE! 🙂
Now you can jump between the editor window and Quickfix window, using the usual Ctrl + w + w command and fix all the errors.
Once done, you can compile again right from inside Vim using :make. More errors? Fix them. And the edit-compile-edit cycle continues 🙂