Vim typically has a number of common compiler configurations installed, as mentioned, so it will automatically choose the appropriate one. To actually use the compiler configuration you need to use the Vim :make function, as also mentioned, but the default mode of operation expects a Makefile
to exist.
If you're just trying to do a quick compile on the current file/buffer (without an existing Makefile) then if you have GNU make installed you can just compile it like this (as explained here):
:make %:r
And it will compile the file and provide vim with the errors/warnings so you can navigate each one using the quickfix (:help quickfix
) list - :cn
Next error, :cp
Previous error, :cw
New window listing errors.
If you don't have GNU make installed you can set this variable - either immediately in the current session like this:
:se makeprg=gcc\ -o\ %<\ %
Or put it in your ~/.vimrc file:
set makeprg=gcc\ -o\ %<\ %
Then you can type:
:make
And it will compile the file and provide you with the quickfix list of errors/warnings in Vim.
EDIT: If you also want to run the compiled executable from within vim you can do ('!' executes, '%:r' is the filename without its suffix):
:!./%:r