2010-03-08 320 views
4

在正常情況下,我使用vim的make工具,我將makeprg設置爲我目前工作的項目的Makefile。由於通常該項目將持續數週甚至更長時間,因此我不需要經常更改makeprg的設置。但有時我需要編寫一些「foobar」代碼,用於練習我的C++技能或在我的腦海中對原始想法進行原型設計。所以每當我切換到VIM使用的「FOOBAR」的模式,我需要評論的原始makeprg設置添加新設置如下:如何在vim中臨時設置makeprg

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

這真的是非常非常不方便。當我回到vim使用的「正常項目模式」時,我需要改回原來的設置。來回......

我想從你們那裏知道的是:是否可以暫時設置makeprg。例如,定義一個函數,首先設置makeprg的本地值,然後在返回之前調用make,形成函數調用,自動將makeprg恢復爲函數調用之前的值。

回答

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++\ % 
7

這不完全是你問什麼,但你可以設置本地只有一個緩衝區選項。這樣你就不必費心打包你的函數。只需在您想要的特定文件上本地更改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++\ %