Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map function keys to compile my program?

Tags:

vim

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.

like image 441
user458553 Avatar asked Oct 08 '10 12:10

user458553


2 Answers

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.]

If you're using an alternative build system, such as Boost Build, SCons, et cetera, then I humbly recommend using Makeshift to set 'makeprg' for you.

Help topics in Vim to get you underway:

  • :help :make
  • :help :nnoremap
  • :help :set
  • :help 'makeprg'
  • :help :_%
like image 173
Johnsyweb Avatar answered Sep 27 '22 23:09

Johnsyweb


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

like image 35
Benoit Avatar answered Sep 27 '22 22:09

Benoit