1

My current keybinding for compiling cpp files is:

autocmd filetype cpp nnoremap <F2> :!g++ -g %<CR>:!./a.out<CR>

When I press F2, vim escapes to the shell and a.out executes well uptil taking input. But, it does not wait for output and returns back to the editor. This never happened earlier when I typed the command manually, vim used to wait for me to read the output and then I got a line saying: "Press ENTER or type command to continue."

[Update] I changed the command to:

autocmd filetype cpp nnoremap <F2> :!g++ -g %<CR>:!./a.out

Now when I press F2, it finally writes :!./a.out below and I manually press Enter. This time it actually waits for me in the terminal. Why is this happening?

Try reproducing by using both the keybindings this one and the former.

1
  • 1
    Your command seems to work for me; that being said: in general, I avoid putting any other Vim commands on the same line after a shell command in :![cmd] (especially in mappings) because I'm not really smart enough to remember all the escaping rules and such, and sometimes it behaves unexpected. You can use a single shell command with nnoremap <F2> :!g++ -g % && ./a.out<CR>. – Martin Tournoij Apr 2 '20 at 23:04
1

I somehow figured out what was going on.

The problem was due to a trailing whitespace at the very end of the keybinding. That was acting as extra key to escape back from the terminal. Removing it resolved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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