How do I run a C program from VIM? How do I run a C program from VIM? c c

How do I run a C program from VIM?


:!gcc -o somename % && ./somename
When using :!, % will be substituted by the name of the currently opened file.

When your project becomes larger, you can also write a makefile and compile the current project with :make, if there are any errors, vim will jump to them automatically.


Use the following mapping code in your .vimrc file for compiling and running a c programming file.

 map <F8> : !gcc % && ./a.out <CR>

F8 key is for run the mapping. "%" is to take the current file name.

Or, if you want to save the current file before compiling it, use

map <F8> :w <CR> :!gcc % && ./a.out <CR>

Or more ideally, if you want to use the file basename not the default 'a.out' as the executable file name, use the following:

map <F8> :w <CR> :!gcc % -o %< && ./%< <CR>

In the above command, "<" after "%" removes extension and dot (foo.c => foo), so "%<" is the file basename.

You can find this and similar infos in cmdline.txt. Command in vim:help: cmdline.txt. You can also find specific details about the use of "%" by using :help filename-modifiersin vim.


TL;DR No Makefile is required, tweaking &makeprg is also completely useless, and yet :make %< is enough to compile from Vim.

Long answer:

I duplicate an answer I gave in a closed "duplicate question".

Considering we are using vim, and not vi, :make is the way to go.

On Linux-like (it also applies to cygwin, but not to mingw on windows -- in mingw case, see the other answers that alter &makeprg, leave it alone otherwise) systems where gnumake is installed, if you don't have a Makefile in your project, and if your project is made of only one file, just type :make %<. It will be enough (you can play with $CXXFLAGS, $CFLAGS, $LDFLAGS (for -Llib/path options) and $LDLIBS (for -llibname options) to tune the compilation options). Then to run the program, type :!./%<, or with latter versions of vim, run :terminal ./%<.

If your project is made of several files, then you'll need a Makefile to take advantage of :make.

If you manage your project with CMake, and if you compile your project in a directory (or several -> debug, release, ...) outside the sources tree, then the integration will require a plugin. AFAIK, I'm the only one to propose such a plugin: BuildToolsWrapper integrates the management of CMake (choice of the build directory, possibility to chose between the debug, or release, or whatever build directory). It has to be coupled with one of the local_vimrc plugin.

In all cases, calling directly the compiler from within (or outside) Vim with :!g++ -o %< % or whatever is what we used to do 15 years ago on vi. Vim has a wonderful feature: it can integrate (yes, like in IDE) the compiler. See :h quickfix. Navigating between errors directly from the editor is much easier than extracting one error line with our eyes, typing back the line number into the editor, going back to the shell to see what exactly was rejected, ... It may be enough in C, but In C++ when we are "trying to call an overload that doesn't exist", we can't work this way (switching back and forth between the editor and the shell).

Finally, if you want to compile on a single keystroke those mono-file projects, you can add in your .vimrc:

nnoremap <silent> <f7> :make %<<cr>

If you want to adapt automatically the compilation command depending of the kind of project mono-file pet project, or real world multi-file project, well, more wiring is needed, and this is what BTW does -- it reads various options to know what to do.

Last note: &makeprg is best left alone, at least not set to g++/gcc/clang/clang++/gfortran/... Because, every time you change your language, you'll have to change it (unless you use :setlocal). With the solution I recommend, if I want to use clang++ instead of g++, all I have to do is to set: :let $CXX='clang++' (or $CC in C), and then call :make %<. I can even define :let $CXXFLAGS='-std=c++11' to compile in C++11 -- the same variable will be used to turn warnings on, to use a sanitizer, etc.