1

This question is quit similar to Vim [compile and] run shortcut

but what I want goes a little further. Is it possible to make a shortcut which compile and run the c code in the build in terminal and leave it open afterwards? The solution in the linked post just closes the output afterwards.

Havsula
  • 43
  • 1
  • 9

2 Answers2

1

I guess the trick we used when coding Turbo Pascal and Turbo C++ would solve your problems. Just add a line for some dummy user input in the end of the program.

int main(void)
{
    // Your code

    getchar(); // Will not return to Vim before you have entered some data
}
klutt
  • 30,332
  • 17
  • 55
  • 95
  • 1
    Hello. This fix will solve part of my problem but I still want to run the program in build in terminal (normally opened with :term command) – Havsula Aug 10 '20 at 08:43
  • 1
    @Havsula Yes, I assumed it's not exactly what you want, but I thought it was worth writing it anyway. – klutt Aug 10 '20 at 08:44
1

You can combine !g++ % -o %< and :vert term ./%< together and make it a shortcut.

Here, ! allows to run external command from vim. g++ will compile the file, % indicates the current file, < is used to remove the file extension. :vert term is an internal command that lets you use terminal within vim.

Put the code in .vimrc file in home directory. The both commands combined would like,

map <F8> :w <CR> :!g++ % -o %< <CR>:vert term ./%<<CR>

When F8 button is pressed, vim saves the file then creates the object code. Afterward, with second command vim opens a terminal and runs the program. You will have to :q or type exit to close the terminal.

Make sure to exit insert mode before you hit F8.

  • yeah but how do i handle c programs that use scanf? vim always send eof and i can't find a workaround – avkat Jan 23 '23 at 23:13