I am using F5 to compile my programs
From my .vimrc

set makeprg=make\ -f\ ~/makefile\ %<
noremap <F5> :make<CR>

But I want to add one other way to compile my programs which compiles a bit slowly and use some other key say F4. I tried inserting another such statement below these lines but then F5 also compiles with the new makefile.

As asked in comments:

set makeprg=make\ -f\ ~/makefile\ %<
noremap <F5> :make<CR>
" slow compilation but more flags for more checks
set makeprg=make\ -f\ ~/makefile1\ %<
noremap <F4> :make<CR>
  • Can you show the <F4> binding? If you change 'makeprg', it affects all :make invocations
    – D. Ben Knoble
    Jan 29 '20 at 15:59
  • @D.BenKnoble Edited my question
    – kayush
    Jan 29 '20 at 16:04
  • Ok, yah, makeprg is just a setting. Mappings don’t save settings at their creation time.
    – D. Ben Knoble
    Jan 29 '20 at 16:14
  • 1
    Actually, it may look more like this (didn't notice the %< at first): :noremap <F4> :exe "make -f ~/makefile1 " . expand('%<')<CR>
    – B Layer
    Jan 29 '20 at 17:03
  • 1
    Yes it works. Sure.
    – kayush
    Jan 29 '20 at 17:16

My understanding is that the makeprg setting is primarily used to change the actual make/build program and in your case the program isn't changing...it's always 'make'.

Since :make accepts arguments I suggest leaving 'makeprg' alone and passing your build command args to :make in the two mappings...

noremap <F4> :make -f ~/makefile1 %<<CR>
noremap <F5> :make -f ~/makefile %<<CR>

Regarding %<, which expands to the current filename minus its extension, in my comments I wavered on whether this would work or if you would need to use :exe and call expand() like so....

noremap <F4> :exe "make -f ~/makefile1 " . expand('%<')<CR>

Actually, you should be okay with the shorter versions.

BTW, %< is there for backwards compatibility with a very old version of vim, per :h :_%<. The synonym, :r, is now "preferred".

  • ? I didn't get you
    – kayush
    Jan 29 '20 at 17:30
  • Thank you for help
    – kayush
    Jan 29 '20 at 17:31
  • You bet. Cheers.
    – B Layer
    Jan 29 '20 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.