I am writing a lot of c++ in vim. I am looking for a way to compile and run the current project including all .cpp-Files in the directory that I am currently in.

There are already several threads about this topic, but they either treat one single .cpp-File or are using a Makefile for building and running. I would want to avoid Makefiles. my goal is to just run something like:

g++ *.cpp -o [Filename]

with a keybinding from Normalmode.

Is there anybody that could help me with this issue?

Thanks in advance!


  • Checkout the help on :make and ‘makeprg’. Alternatively, your mapping could « shell out » using !g++ ... the advantage of :make and related options (which dont necessitate makefiles!) is that you get errors in quickfix. – D. Ben Knoble Dec 9 '17 at 15:57
  • 4
    You are in the start of a long, long way. :-) Your code will be bigger and bigger, and your script will compile it slower and slower. And you will have additional tasks to make: processing some config files/databases, checking for the availability of things and so on. So you will extend your single-command compilation to a more complex script. But so it will be slow. Then you will want to use the compilation on all of your cpus, and you will need the feature to recompile only the changed files... and then the GNU Make will gladly welcome you back. :-) – peterh - Reinstate Monica Dec 9 '17 at 17:29

:make doesn't have to actually use a Makefile.

Try something like:

:let &makeprg = "g++ *.cpp -o [Filename]"

You can then run the command with :make and if desired you can set up a normal mode mapping for this with something like:

:nnoremap <leader>m :make<CR>

See :help 'makeprg' for more details.

You'll probably also want to ensure 'errorformat' is set up appropriately so that the quickfix window is filled correctly (having a look at $VIMRUNTIME/compiler/gcc is probably a good start), and you might also like to look into :help compiler-select.

| improve this answer | |

Sorry, I've misread your request. However, if editing a source file means, that only a single binary has to be updated my answer below is helpful. If your project has grown to consist of several binaries which depend on shared source code, this definitely sounds like you need a build tool and I will give you a quick start in my new answer:

Build Tool: CMake

If you do not like GNU Make, or the Makefiles in particular, consider CMake as tool to generate Makefiles for you. To make it more clear: vim is not a build tool!

A simple setup with CMake would be to create the file CMakeLists.txt in your project folder

cmake_minimum_required (VERSION 3.0)
project (Tutorial)
add_executable(binaryname1 main1.cpp)
add_executable(binaryname2 main2.cpp)
add_executable(binarynameN mainN.cpp)

or if your binaries have the same name as your cpp files and they are located in the main folder, you can use

file( GLOB APP_SOURCES *.cpp )
foreach( testsourcefile ${APP_SOURCES} )
    string( REPLACE ".cpp" "" testname ${testsourcefile} )
    add_executable( ${testname} ${testsourcefile} )
endforeach( testsourcefile ${APP_SOURCES} )

Some build tools do not support wildcards to detect source files: http://mesonbuild.com/FAQ.html#why-cant-i-specify-target-files-with-a-wildcard

Create the Makefile with $ cmake .. The created Makefile compiles all of your binaries with one invocation of $ make or within vim with :make.

And as a beginner you should be aware of the following: you do not need to re-run cmake as long as you do not change the file CMakeLists.txt. For example, editing a shared source file in vim and simply calling :make is sufficient to update all of your binaries. CMake has automatically detected the dependencies and creates a sensible Makefile, so that the Makefile targets will be considered as out-of-date by your file change.

Alternative build tools:

Old answer:

Plugin SingleCompile

If you do not mind using plugins, there are also several ones for the situation where the compilation is straightforward (no configuration of linking, includepaths, ...). One to consider is called SingleCompile and is available from http://singlecompile.topbug.net/ and on github. The suggested mappings are

nmap <F9> :SCCompile<cr>
nmap <F10> :SCCompileRun<cr>


If you like to run and interact with your compiled program see this discussion on reddit https://www.reddit.com/r/vim/comments/6vfrrz/plugin_to_compile_and_run_file/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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