如何在vim中临时设置makeprg

时间:2010-03-08 21:51:44

标签: vim

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

au FileType c set makeprg=gcc\ %
au FileType cpp set makeprg=g++\ %

这真的非常不方便。当我回到vim使用的“正常项目模式”时,我需要改回原来的设置。来回....

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

2 个答案:

答案 0 :(得分:8)

这并不是您要求的,但您可以只为缓冲区设置本地选项。这样你就不用费心去包装你的功能;只需在您想要的特定文件上本地更改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++\ %

答案 1 :(得分:5)

如果你想在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

你也可以将你的'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++\ %