Reputation: 43
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.
Upvotes: 1
Views: 1703
Reputation: 45
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.
Upvotes: 1
Reputation: 31306
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
}
Upvotes: 1