开发者

Set “makeprg” via modeline in Vim

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开发者_如何学JAVA’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.


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.


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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜