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.