tags:

views:

75

answers:

3

Hello

In VIM i want to map say function key Fn for compiling the currently open C/C++ file. Is that possible ? If yes please guide me.

+2  A: 

yes it is possible. Do you have a Makefile? Then insert this into your vimrc:

nnoremap <f1> :make<return>

This remaps the F1 key to invoke the internal command “make” of vim, which if you're running on a Linux machine will call gnu Make.

For more help, run :help :make and :help :nnoremap

Benoit
+3  A: 

It is certainly possible and is my preferred way of working.

I have the following in my .vimrc:

nnoremap    <F5>   :make<CR>

This will call 'makeprg', which defaults to 'make'. Then you can use the results in Vim's Quickfix mode to tackle compilations errors, warnings, etcetera, which (with the correct setup) will deliver your cursor right to where the error lies in your code.

If you just want to compile the current file, you could set 'makeprg' to something other than 'make', such as your compiler, followed by the current file:

:set makeprg=g++\ %

[but then you'll need to add compiler flags such as include paths, etc.]

Help topics in Vim to get you underway:

Johnsyweb
`<C-U>` is not necessary with nnoremap. It could be with x*map and v*map but in normal mode, : starts an empty command without range.
Benoit
Do you know, that has been in my `.vimrc` for so long, I've forgotten why I'd put it there! Answer updated.
Johnsyweb
See also http://vimdoc.sourceforge.net/htmldoc/quickfix.html#compiler-select for some pre-configured compiler commands (and error parsers) which make it easy to use the quickfix list with things other than ``make``+``gcc`` .
David Winslow
Thanks David. Added your link to my answer.
Johnsyweb
+2  A: 

Fully subscribing the Johnsyweb's answer, I would also recommend to take a look at SingleCompile plugin. If it's sufficient for you to quickly compile single-file program, this plugin could just suit your needs.

ib