This question already has an answer here:

I am using vim editor on Linux mint. I want to know if there is any way to compile c program without leaving the editor.

marked as duplicate by Bernhard, slm linux Jun 20 '14 at 14:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


There are several possibilities.

One method is to compile using

:!gcc file.c

But a nicer strategy would be to have a Makefile and compile just using


Where the easiest Makefile would look like.

        gcc file.c

Others can explain this a lot better.

  • 1
    According to Is there a vim shortcut for <name of current file>?, :!gcc % seems handier. – manatwork Oct 22 '12 at 12:21
  • 1
    @manatwork That only works if the current file is actually the main program. – Bernhard Oct 22 '12 at 12:28
  • can you elaborate on the Makefile strategy please... – user1614244 Oct 22 '12 at 12:32
  • Correct. For other cases your second code should be preferred. – manatwork Oct 22 '12 at 12:32
  • 1
    @user1614244 - step 1) learn to use make, step 2) invoke :make. Note however that you can set makeprg to the build tool of your choice – Useless Oct 22 '12 at 14:24

The canonical way to do this in Vim is to use the compiler configuration setting. Your vim installation almost certainly comes with a compiler plugin for gcc. Type :help compiler in Vim to find out more about how this works.

To associate gcc with c source files, you need something like this in your .vimrc:

au BufEnter *.c compiler gcc
  • 7
    How would you go about invoking the compiler once you have set it? – Alex Chamberlain Oct 22 '12 at 22:13

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