2012-08-02 17 views
3

如何優雅的方式使用單.vimrc文件(無文件類型插件)改變Vim的行爲按文件類型改變Vim的行爲?使用單`.vimrc`(無文件類型插件)

這是什麼意思是......如果我跑對C許多讚揚/ C++文件,例如:

set nu 
set cin 
set ai 
set mouse=a 
color elflord 

和AsciiDoc文件讚揚的另一個一堆,如:

set syntax=asciidoc 
set nocin 
set spell spl=en 

沒有提到Python,LaTeX等......

存在解決方案 https://stackoverflow.com/a/159065/544721 for put autocommand每個自定義命令之前。

有沒有一種很好的方式將它們分組爲autocommand而不使用ftplugin - 爲了保持所有內容都在單個.vimrc文件中(更適合在多臺機器上移動等)? 東西相當於if語句括號{}

+0

請參閱:http://stackoverflow.com/questions/2889766/best-way-to-organize-filetype-settings-in-vim-and-vimrc – pb2q 2012-08-02 20:41:06

回答

3

您可以使用以下方法:

if has("autocmd") 
    augroup LISP 
     au! 
     au BufReadPost *.cl :set lisp 
     au BufReadPost *.cl :set showmatch 
     au BufReadPost *.cl :set cpoptions-=m 
     au BufReadPost *.cl :set autoindent 
    augroup END 
    augroup C 
     au! 
     autocmd BufNewFile,BufRead *.cpp set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4 
     autocmd BufNewFile,BufRead *.c set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4 
     autocmd BufNewFile,BufRead *.h set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4 
     autocmd BufNewFile,BufRead *.cpp set tw=80 
     autocmd BufNewFile,BufRead *.c set tw=80 
     autocmd BufNewFile,BufRead *.h set tw=80 
    augroup END 
endif 

這創造依賴於所打開的文件類型,這是在自動命令部分指定命令的分組。您仍然需要在每個之前指定autocmd或au,但它們很好地分組。

3

我可能會做的每個文件類型的功能做設置爲我。無恥地剝去@Derek ...

function! SetUpLispBuffer() 
    set lisp 
    set showmatch 
    set cpoptions-=m 
    set autoindent 
endfunction 

function! SetUpCBuffer() 
    set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4 
    set tw=80 
endfunction 

if has("autocmd") 
    augroup LISP 
     au! 
     au BufReadPost *.cl call SetUpLispBuffer() 
    augroup END 
    augroup C 
     au! 
     autocmd BufNewFile,BufRead *.{cpp,c,h} call SetUpCBuffer 
    augroup END 
endif 

少了很多改變,當你想進行修改和更切&糊了。

+0

我有點新鮮...我似乎讀「金」!刪除此文件類型組的自動命令。我們爲什麼要做這個?這可能會干擾其他vim插件嗎?謝謝:) – aikeru 2014-09-10 13:42:18

+1

'au!'刪除包含augroup中的自動命令,所以只需將它們命名爲不會與插件衝突的內容。 (例如,我的augroups通常被命名爲TomsFileCleanups等) – 2014-09-24 00:37:12