7. Quickfix

When coding in C one often has a edit-compile-edit cycle. Typically you would edit C file using some the things I've mentioned earlier, save the file, compile the code and go to the error(s) and start editing again. VIM helps save the cycle time slightly using a mode called quickfix. Basically, one has to save the compiler errors in a file and open the file with VIM using the command

        $ vim -q compiler_error_file
      

VIM automatically opens the file containing the error and positions the cursor at the location of the first error.

There is a shortcut to the cycle. Using the command "make", one can automatically compile code and goto the position where the first error occurs. To invoke the make command type the following

        :make
      

Basically, this command calls make in a shell and goes to the first error. However, if you are not compiling using make and are compiling using a command such as cc, then you have to set a variable called makeprg to the command you want invoked when you use the make command. For eg. :set makeprg=cc\ foo.c

After setting makeprg, you can just call the make command and quickfix will come into play.

After you have corrected the first error, the next thing to do would be go to the next error and correct that. The following command is used go to the next error. :cn

To go back, you can use the command :cN

Let me demonstrate this using an example. Consider the following code

Figure 11. Quickfile Program Listing

As you can see there is an error on line number 5. The file is saved as test.c and makeprg is set using the command

        :set makeprg=gcc\ test.c
      

Next the make command is invoked using the command :make. gcc gives an error and the output of the make command is something like this

Figure 12. :make error

On pressing RETURN, the cursor moves to line number 6.

Now, the command :cn will move the cursor to the line number 4.

To move back to the previous error, one can use the command :cN and the cursor will move back to the line 6.

After correcting the error on line 5 and adding "return 1;", one can run :make again and the output will be

Figure 13. No Error

That was just a small example. You can use quickfix to solve your compile time problems and hopefully reduce the edit-compile-edit cycle.