简体   繁体   English

在Vim中通过modeline设置“ makeprg”

[英]Set “makeprg” via modeline in Vim

By default, makeprg is set to just make . 默认情况下, makeprg设置为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 . 当然,这需要在调用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. 例如,我有一个通过调用make foo来构建源文件<foo>.cpp的项目,该项目包含许多此类源文件。

How can I achieve that when I'm editing said <foo>.cpp in Vim, makeprg is set to make <foo> ? 当我在Vim中编辑说<foo>.cpp时, makeprgmakeprg设置为make <foo> I thought of modelines in each file, but setting makeprg via the modeline is forbidden (due to security concerns?). 我想到了每个文件中的模式行,但是禁止通过modeline设置makeprg (出于安全考虑?)。

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 请注意,此设置仅适用于某些项目,因此我不想修改make常规值–否则,我只是

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

(or something like that) in my .gvimrc . (或类似的东西)在我的.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. 这样,我的.vimrc不会因项目特定的内容而混乱。 Instead, in each project root directory I have a _vimrc_local.vim file that contains my various settings. 相反,在每个项目的根目录中,我都有一个_vimrc_local.vim文件,其中包含我的各种设置。

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 . 另一个解决方案是将:make调用封装到另一个命令或映射中,然后使用一个可以通过另一个插件let -modeline设置的变量来调用:make

PS: the .gvimrc is meant to contain things specific to the graphical version of vim. PS: .gvimrc旨在包含特定于vim图形版本的内容。 By putting your settings into your .gvimrc , plain vim won't be configured. 通过将设置放入.gvimrc ,将不会配置纯vim。

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

It should be possible to something similar with autocommands, if you can distinguish your project not by modeline but by their folder. 如果您不是通过modeline而是通过其文件夹来区分项目,则应该可以使用与自动命令相似的工具。 The following is a combination of an example from :help autocmd-patterns and your example code. 以下是来自:help autocmd-patterns的示例和您的示例代码的组合。

: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) (请注意,我还没有测试这个确切的命令,相反,我有一些自动命令来设置makeprg,具体取决于我的vimrc中的文件类型)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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