tags:

views:

73

answers:

3

By default, makeprg is set to just make. However, I’ve got a lot of projects where different build configurations and targets reside in the same directory.

This of course necessitates that I specify a target when invoking make.

For example, I’ve got a project that’s set up to build a source file <foo>.cpp by invoking make foo, and the project contains a number of such source files.

How can I achieve that when I’m editing said <foo>.cpp in Vim, makeprg is set to make <foo>? I thought of modelines in each file, but setting makeprg via the modeline is forbidden (due to security concerns?).

Notice that this setting is only appropriate for some projects so I don’t want to modify the general value of make – otherwise I’d just

set makeprg=make\ expand("%:t:r")

(or something like that) in my .gvimrc.

+1  A: 

You should be able to map :make %< which I believe will do what you want

Hasturkun
+1  A: 

I'm using a plugin of mine that enables project-specific settings to tune that kind of things. This way my .vimrc is not cluttered with project-specific things. Instead, in each project root directory I have a _vimrc_local.vim file that contains my various settings.

Another solution would be to encapsulate :make call into a another command or a mapping that in turn calls :make with a variable that you could set with another plugin: let-modeline.

PS: the .gvimrc is meant to contain things specific to the graphical version of vim. By putting your settings into your .gvimrc, plain vim won't be configured.

Luc Hermitte
@Luc: re the `.gvimrc` file: of course, I’m aware of that. I’m using both, my question just wasn’t consistent. That plugin looks promising – in fact, I believe that’s exactly what I’m looking for.
Konrad Rudolph
A: 

It should be possible to something similar with autocommands, if you can distinguish your project not by modeline but by their folder. The following is a combination of an example from :help autocmd-patterns and your example code.

:autocmd BufRead /source/foo-project/*.cpp set makeprg=make\ expand("%:t:r")

(Note that I haven't tested this exact command, instead I have some autocommands for setting makeprg depending on the filetype in my vimrc)

Jörn Horstmann
In a previous (similar, but deleted it seems) anwser, the OP said he'd rather not clutter his .vimrc with project-specific settings.
Luc Hermitte
@Jörn: Thanks, but Luc’s right.
Konrad Rudolph