2012-06-14 37 views
16

我在裝有Vim 7.2和7.3的機器之間使用相同的.vimrc。用Vim 7.2的機器每次抱怨我的7.3特定的選項我打開一個文件:忽略vimrc中的「未知選項」錯誤

Error detected while processing /home/spiffytech/.vimrc: 
line 72: 
E518: Unknown option: rnu 
line 73: 
E518: Unknown option: undofile 
line 74: 
E518: Unknown option: undodir=/tmp 
line 75: 
E518: Unknown option: cryptmethod=blowfish 
Press ENTER or type command to continue 

我怎樣才能使Vim忽略這些錯誤,並沒有提示我鍵入回車,每當我打開的文件?

回答

10

總結新的選項:

if version >= 703 
    set rnu ... 
endif 

檢查幫助對於v:version欲瞭解更多版本號的信息,請使用:

         *v:version* *version-variable* 
v:version  Version number of Vim: Major version number times 100 plus 
       minor version number. Version 5.0 is 500. Version 5.1 (5.01) 
       is 501. Read-only. "version" also works, for backwards 
       compatibility. 
       Use |has()| to check if a certain patch was included, e.g.: > 
         if has("patch123") 
<    Note that patch numbers are specific to the version, thus both 
       version 5.0 and 5.1 may have a patch 123, but these are 
       completely different. 
+0

工程。一定要小心設置版本號 - 花了一段時間讓我足夠仔細閱讀,看看它不*僅僅是「版本* 100」 – spiffytech

+0

是的,這可能會很棘手,但你會習慣它。我寧願用字符串連接意義來描述它,而不是數學。請注意,這用於許多其他地方。例如,Perl在['use'](http://perldoc.perl.org/functions/use.html)中使用了類似的語法。你必須使用5.010;'使用不是'5.1'的perl'5.10'中的特性。這就是爲什麼他們不使用十進制的意思,否則會造成一些模糊性。 – sidyll

+2

最好是測試比版本的特定功能,因爲可以編譯特定版本而不使用您正在查找的功能。 – cpbills

3

在你的.vimrc中,你可以測試你正在執行的Vim版本。

help v:version

if v:version >= 703 
    "do something 
    set rnu 
    set undofile 
    ... 
endif 

對應的Vim 7.3(這是不是真的......直觀)

0

我會說這個問題沒有回答。考慮在具有較新vim版本的計算機A上創建的Session.vim。在另一臺計算機B嘗試打開Session.vim時,在源代碼管理中觸發錯誤。必須手動包裝版本號纔是自動化過程。有了這種行爲,新版本在保存會話時必須自動在版本號中包裝新命令 - 這是7.3不能做到的。

19

對於實際支持的功能而不是版本,可能需要進行更細粒度的檢查。

例如爲:

if has('persistent_undo') 
    set undofile 
    set undodir=/tmp 
endif 

" Some options can only be checked with exists('+option'); I'm not sure why 
if exists('+relativenumber') 
    set rnu 
endif 

if has('cryptv') 
    set cryptmethod=blowfish 
end 
3

可以忽略任何錯誤與silent! ...,像silent! set undofile

7

有時一個選項是合法的,但在當前的環境下無法使用。例如:

$ vi 
Error detected while processing /home/username/.vimrc: 
line 9: 
Unknown option: indentexpr= 

要測試的選項是否存在,並避免一個錯誤,如果沒有可用的:

if exists("&indentexpr") 
    :set indentexpr= 
endif 
+1

這是解決問題的最佳方法。 –

+0

這並沒有擺弄版本號。 – iltempo