1

I had the following remap

autocmd filetype cpp nnoremap <F5> :w <bar> silent !g++ -ulimit -Wall -Wno-unused-result -std=c++11   -O2   % -o %:r && %:r<CR>

When I run this in Vim, a terminal like this shows up vim output

But in Neovim, I get this neovim output

If I have 2 remaps, one for compiling and one for running, I get shell returned with 139. I want to get the above functionality in Neovim.

These are the commands that I used

autocmd filetype cpp nnoremap <F9> :w <bar> !mkdir -p .outputs && g++ -std=c++17 -O2 -Wshadow -Wall % -o .outputs/%:r -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG<CR>
autocmd filetype cpp nnoremap <F12> :!.outputs/%:r<CR>
autocmd filetype cpp nnoremap <F5> :w <bar> !g++ -ulimit -Wall -Wno-unused-result -std=c++11   -O2   % -o %:r && %:r<CR>
1

1 Answer 1

0

I doubt you're just typing nameofthexecutable in the console. It's the same for Vim, the path to that executable is required if it isn't in $PATH ==> ./nameofthexecutable


Otherwise, we have a better user experience in Vim to compile C++ when we use the quickfix feature. In particular when there are hundred of error lines because we are sending an object to std::cout and have forgotten to overload the operator<< for its type.

Unless your flavour of make is incorrectly configured, there is no need to play with &makeprg, we can keep it simple and just set $CXXFLAGS, $LDFLAGS and $LDLIBS, make will take care of everything for single file programs.

" ~/.vimrc
filetype plugin on


" ~/.vim/ftplugin/cpp.vim
" ftplugins are easier to maintain when there are several definitions
let $CXXFLAGS = '-ulimit -Wall -Wno-unused-result -std=c++11 -O2'

" - don't forget <buffer> for ft-specific mappings
" - using the quickfix feature for vim really make the difference when compiling C++
" - I use a trick to inject the execution at the end of the execution
nnoremap <silent> <buffer> <F5> :<c-u>w <bar> :make %< && ./%<

However, there is a little issue to chain compilation and execution. Either we use the little trick I've used just above, or we add a missing feature. See https://stackoverflow.com/a/56991040/15934

7
  • No. I've tested it this way. I have the same vimrc set up both for neovim and vim. It works in vim but not in neovim My init.vim: https://pastebin.com/ZrgGajG0 FYI: The autocommands are in the end
    – dinskid
    Commented Jun 17, 2020 at 17:16
  • By "tested this way"? You mean you've tested the 3 points I've suggested: with an explicit path to the executable, and the quickfix feature, and with an ftplugin to avoid poor side effects from direct use of autocommands? Commented Jun 17, 2020 at 19:30
  • I'm sorry for the confusion. I am saying the same vimrc works as shown in vim but doesn't work the same way as vim in nvim. The problem was me expecting the same terminal from both vim and nvim. Can you please expand on your answer as to what you meant by the ftplugin/cpp.vim.. I am a newbie to vim and nvim
    – dinskid
    Commented Jun 18, 2020 at 20:54
  • (Indirectly) Using the terminal as you are doing is what we had to do with Vi. Not Vim. Vi! Vim provides (from the very beginning) a way to integrate compilation. See :h :make. If you follow the link I gave you'll have explanations. Regarding ftplugin, they are a dedicated mechanism since Vim 6.0 to define filetype related things (mappings, etc). :h filetype for more. I gave the minimum required code snippets for your ~/.vimrc (or neovim equivalent), and a simplified content for ~/ftplugin/cpp.vim. Autocommands are the low level feature, that have some side-effects sometimes. Commented Jun 18, 2020 at 21:53
  • I set it up the way you said now. The problem I have now is I am not able to interact with it. I want to run the executable the way it runs in a terminal(it should receive inputs and be interactive). I am sorry, I am a newbie
    – dinskid
    Commented Jun 20, 2020 at 13:08

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.