如何在vim中临时设置makeprg

如何在vim中临时设置makeprg,vim,Vim,在正常情况下,我使用vim的make实用程序,将makeprg设置为我当前工作的项目的Makefile。由于项目通常会持续数周甚至更长时间,我不需要经常更改makeprg的设置。但有时我需要编写一些“FoBar”代码来练习我的C++技巧或者在我的头脑中原型化一些原始的想法。因此,每当我切换到vim使用的“foobar”模式时,我需要对原始makeprg设置进行注释,并添加新设置,如下所示: au FileType c set makeprg=gcc\ % au FileType cpp set

在正常情况下,我使用vim的make实用程序,将makeprg设置为我当前工作的项目的Makefile。由于项目通常会持续数周甚至更长时间,我不需要经常更改makeprg的设置。但有时我需要编写一些“FoBar”代码来练习我的C++技巧或者在我的头脑中原型化一些原始的想法。因此,每当我切换到vim使用的“foobar”模式时,我需要对原始makeprg设置进行注释,并添加新设置,如下所示:

au FileType c set makeprg=gcc\ %
au FileType cpp set makeprg=g++\ %
这真的很不方便。当我回到vim使用的“正常项目模式”时,我需要更改回原始设置。来回


我想从你们这里知道的是:是否可以临时设置makeprg。例如,定义一个函数,其中首先设置makeprg的本地值,然后在函数调用返回之前调用make,自动将makeprg恢复为函数调用之前的值。

如果要在vim中的函数调用之前/之后保存和恢复选项,请执行以下操作:

let oldmakeprg = &l:makeprg try " set new value of makeprg and call the function set makeprg=new\ value call MyFunction() finally " set makeprg back to old value let &l:makeprg = oldmakeprg endtry 让oldmakeprg=&l:makeprg 尝试 “设置makeprg的新值并调用函数 设置makeprg=new\value 调用MyFunction() 最后 “将makeprg设置回旧值 let&l:makeprg=oldmakeprg 末日 您还可以将“foobar”代码放在一个特殊文件夹中,并使用单独的自动命令分别设置makeprg:

" normal settings for makeprg au FileType c set makeprg=gcc\ % au FileType cpp set makeprg=g++\ % " special settings for foobar code au BufRead,BufNewFile **/foobar/**.c set makeprg=gcc\ % au BufRead,BufNewFile **/foobar/**.cpp set makeprg=g++\ % “makeprg的正常设置 au文件类型c集合makeprg=gcc\% au文件类型cpp集makeprg=g++\% “条形码的特殊设置 au BufRead,BufNewFile**/foobar/**.c set makeprg=gcc\% au BufRead,BufNewFile**/foobar/**.cpp set makeprg=g++\%
这并不完全是您所要求的,但您可以将选项设置为仅本地缓冲区。这样你就不必费心包装你的函数;只需在您想要的特定文件上本地更改
makepgrp

*:setl* *:setlocal* :setl[ocal] ... Like ":set" but set only the value local to the current buffer or window. Not all options have a local value. If the option does not have a local value the global value is set. With the "all" argument: display all local option's local values. Without argument: Display all local option's local values which are different from the default. When displaying a specific local option, show the local value. For a global/local boolean option, when the global value is being used, "--" is displayed before the option name. For a global option the global value is shown (but that might change in the future). {not in Vi}
au FileType c setl mp=gcc\ %
au FileType cpp setl mp=g++\ %