I have some C++ source code that I am running. I have F9 and F10 mapped to the following in my vimrc file:

autocmd filetype cpp nnoremap <F9> :w <bar> !g++ -std=c++14 % -o %:r -Wl,--stack,268435456<CR>
autocmd filetype cpp nnoremap <F10> :!%:r<CR>

Whenever I press F9, a terminal window opens. Afterwards, a .exe file is created in my working directory. Problem is, I don't want this .exe file since it clutters up my workspace. Is there any way I can either turn this off or save specifically the .exe files to a different directory?

1 Answer 1


Your g++ command contains -o %:r; the -o flag for g++ sets the output filename, the % expands to the current buffer name in the Vim commandline, and :r is a modifier to remove any file extension. So for file.cpp it would be file. I assume the .exe part is added by g++ automatically on Windows.

To use a different path, well, specify a different path in there 😅 :help filename-modifiers contains a list of all modifiers you can use with %; and you can test this with expand() or :!echo (don't forget the !! I prefer expand() because it's so easy to forget):

:echo expand('%')
:echo expand('%:r')

I don't know what makes sense for you or your projects, but to store files in a .obj directory you could for example use:

!g++ -std=c++14 % -o .obj/%:r

The downside of this is that it won't work if the .obj directory doesn't exist yet, so you may want to add that to your keybind:

nnoremap <F9> :w <bar> call mkdir('.obj', 'p') <bar> !g++ -std=c++14 % -o %:r -Wl,--stack,268435456<CR>

With the 'p' as the second argument to mkdir() you won't get an error if the directory doesn't exist.

You can also store something in your user profile, temp directory, or some other global location on your system:

!g++ -std=c++14 % -o $TEMPDIR/%:r

I think $TEMPDIR will expand to the temporary directory on Windows, but I'm not 100% sure so you may want to verify this; but the idea should be clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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