简体   繁体   中英

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

您应该能够映射:make %< ,我相信它将做您想要的

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM