Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

marked as duplicate by Bernhard, slm Jun 20 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.

add comment

2 Answers 2

up vote 10 down vote accepted

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

:make

Where the easiest Makefile would look like.

program:
        gcc file.c

Others can explain this a lot better.

share|improve this answer
    
According to Is there a vim shortcut for <name of current file>?, :!gcc % seems handier. –  manatwork Oct 22 '12 at 12:21
    
@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
show 1 more comment

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
share|improve this answer
1  
How would you go about invoking the compiler once you have set it? –  Alex Chamberlain Oct 22 '12 at 22:13
    
got it !!thanks! –  user1614244 Oct 24 '12 at 3:48
add comment

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