Compile and run C/CPP with one click in vim

On 2017/02/20 at 23:26

When practicing coding contest, the need to compile and run a single C++ file very quickly is very often. In vim, I map F7 to call g++ to compile the current file and F8 to run the compiled binary instantly. The % symbol stands for the filename of the current editing file in the vim command mode. I can use the expand('%:e') to extract the file extension and %< for filename without extension. The config snippet is shown below.

if "cpp" == expand('%:e') || "c" == expand('%:e')
    noremap <F7> :! g++ % -o %<
    noremap <F8> :! ./%<