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
Related videos on Youtube
Author by
user1614244
Updated on September 18, 2022Comments
-
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 over 11 yearsAccording to Is there a vim shortcut for <name of current file>?,
:!gcc %
seems handier. -
Bernhard over 11 years@manatwork That only works if the current file is actually the main program.
-
user1614244 over 11 yearscan you elaborate on the Makefile strategy please...
-
manatwork over 11 yearsCorrect. For other cases your second code should be preferred.
-
Useless over 11 years@user1614244 - step 1) learn to use make, step 2) invoke
:make
. Note however that you can setmakeprg
to the build tool of your choice -
Alex Chamberlain over 11 yearsHow would you go about invoking the compiler once you have set it?