5

When I try to get a program to compile, I use the following process:

  1. :sh to open a shell
  2. make
  3. scroll up to see the compilation errors
  4. exit to get back to vim
  5. fix errors that I remember (usually 2 or 3 of them)
  6. goto step 1

This is kind of tedious, ideally I would like to see the errors in a horizontal split pane alongside the code I am editing. However, unlike emacs I cannot open a buffer that has a terminal in it.

How can I improve my process?

7

You should look at :h quickfix.txt.

Vim has a special mode to speedup the edit-compile-edit cycle. This is inspired by the quickfix option of the Manx's Aztec C compiler on the Amiga. The idea is to save the error messages from the compiler in a file and use Vim to jump to the errors one by one. You can examine each problem and fix it, without having to remember all the error messages.

To compile your code use the :make command (:h :make) Then to move between your errors you can use :cnext and :cprevious.

The doc describes the behavior of :make:

  1. If vim was built with +autocmd, all relevant QuickFixCmdPre autocommands are executed.
  2. If the 'autowrite' option is on, write any changed buffers
  3. An errorfile name is made from 'makeef'. If 'makeef' doesn't contain "##", and a file with this name already exists, it is deleted.
  4. The program given with the 'makeprg' option is started (default "make") with the optional [arguments] and the output is saved in the errorfile (for Unix it is also echoed on the screen).
  5. The errorfile is read using 'errorformat'.
  6. If vim was built with +autocmd, all relevant QuickFixCmdPost autocommands are executed. See example below.
  7. If [!] is not given the first error is jumped to.
  8. The errorfile is deleted.
  9. You can now move through the errors with commands like :cnext and :cprevious, see above.

You should also have a look at :h 'makeprg' to define the behavior of :make.

Note that Vim must be compiled with +autocmd enabled.


Some additional commands to navigate the quickfix list are the following:

:cnext  Jump to next item
:cprev  Jump to previous item
:cfirst Jump to first item
:clast  Jump to last item
:cnfile Jump to first item in next file
:cpfile Jump to last item in previous file
:cc     N Jump to nth item
:copen  Open the quickfix window
:cclose Close the quickfix window

You could also be interested in the location-list feature (:h location-list). There can only be one quickfix list whereas there can be several location-list (one by window), thus you can compile two files and switch between them without erasing the location of the errors.

To manipulate the location-list you can use the same commands as the ones used with the quickfix but prefixing them with l: :lmake, :lnext, etc... The commands interacting with the location-list will use the location-list of the current window.


I would also recommend reading Practical Vim which provides some very good tips and has a section dedicated to compilation and using the quickfix list.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.