I want to make a shortcut for compiling C++ file. I use :map <F5> :!g++ % -o app && ./app <CR>. How can I edit it to set output file's name, C++ file's name without .cpp instead of app?

For example I want the name of g++ output file to be main when I use <F5> when I edit main.cpp.

NOTE: I use neovim 0.6.1


1 Answer 1


Check out some related questions: https://vi.stackexchange.com/search?q=%5Bfiletype-c%2B%2B%5D+g%2B%2B, https://vi.stackexchange.com/search?q=%5Bfiletype-c%2B%2B%5D+compile

A short answer: prefer to use :make to build and probably :terminal or :! to run the program, all of which you can bind maps to.

But if you insist on this particular setup: :help cmdline-special and :help filename-modifiers give %:t:r.

Your Answer

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

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