-2

I am using gVim as my text editor and I use it to write C++ and to execute my C++ file I have this map in my .vimrc:

map <F9> :<C-U>!x-terminal-emulator -e ./%:r<CR>

But when I run it the terminal closes immediately after I write my input and I can't see the output, is there a way I can make it work like this: enter image description here

To show me the output then to let me know that I can press return and then exit from the terminal, this is the terminal when I execute code in Geany.

R0L3eX
  • 31
  • 3
  • 4
    You can just add a `read _` or `read -p "press any key " _` or similar command to the end of your shell command. Otherwise check the documentation for the terminal you're using (eg, xterm has a `-hold` option that does what you want, GNOME terminal has [this](https://help.gnome.org/users/gnome-terminal/stable/pref-custom-exit.html.en) etc.) – Useless Nov 18 '20 at 12:11

2 Answers2

1

IMO the best in to use the integrated terminal:

:term ./%<

If you really want to use a terminal that doesn't support an option like -hold which xterm has, you can always add a little helper script in your $PATH and use it from your mapping.

nnoremap <silent> <f9> :<c-u>!x-terminal-emulator -e start.sh ./%<<cr>
#!/bin/bash
# start.sh, to be dropped in $PATH
"$@"
echo "(program exited with code: $?)"
read -p "press return to exit"
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
-1

Just add a cin call at the end of your program. It is the easiest way to deal with such problems on any platform. The other way is checking the documentation of your environment and making some configurations. As far as I know, running the program by F5 in Geany requires pressing the Enter before closing.

  • 1
    I have to disagree with the `cin` approach. Once the console program is executed as designed, i.e. from the console, it will always be parasited with an extra and unwelcomed pause. The issue is on the development environment side, and this is where it shall be fixed. As the OP and you have said, geany already does this correctly as well as `:!./%<` or `:term ./%<` in (g)vim. The issue arises when using a terminal that doesn't wait before closure. – Luc Hermitte Nov 18 '20 at 14:22