<-- home

Compile and run cpp program from Vim

To compile and run cpp program from MVIM(or VIM) directly. Add this to your ~/.vimrc

nnoremap <C-c> :!g++ -o  %:r.out % -std=c++11<Enter>
nnoremap <C-x> :!./%:r.out

Note: You can remove/add the <Enter> if you prefer to press <Enter> yourself.

Now simply hit Ctrl + c to compile and Ctrl + x to execute.

For a source file with name main.cpp, commands will be expanded to

g++ -o main.out main.cpp -std=c++11
./main.out

The %:r gets the current filename without the extension whereas % gets the current filename.

You can very easily extend above approach to compile using makefile.