How to compile a c program without leaving the editor?

61,016

Solution 1

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.

Solution 2

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:
61,016

Related videos on Youtube

user1614244
Author by

user1614244

Updated on September 18, 2022

Comments

  • user1614244
    user1614244 over 1 year

    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.

  • manatwork
    manatwork over 11 years
    According to Is there a vim shortcut for <name of current file>?, :!gcc % seems handier.
  • Bernhard
    Bernhard over 11 years
    @manatwork That only works if the current file is actually the main program.
  • user1614244
    user1614244 over 11 years
    can you elaborate on the Makefile strategy please...
  • manatwork
    manatwork over 11 years
    Correct. For other cases your second code should be preferred.
  • Useless
    Useless over 11 years
    @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
  • Alex Chamberlain
    Alex Chamberlain over 11 years
    How would you go about invoking the compiler once you have set it?