使用单个`.vimrc`(没有ftplugin)按文件类型更改Vim行为

时间:2012-08-02 20:30:29

标签: vim

如何以优雅的方式使用单个.vimrc文件(不使用ftplugin)更改Vim行为?

我的意思是......如果我为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 在每个自定义命令之前放置autocommand

有没有一种很好的方法可以在不使用autocommand的情况下为ftplugin分组它们 - 为了将所有内容保存在单个.vimrc文件中(更适合移动许多计算机等)? 相当于带括号if的{​​{1}}语句的内容?

2 个答案:

答案 0 :(得分: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部分中指定。你仍然需要在每个之前指定autocmd或au,但它们很好地分组。

答案 1 :(得分:3)

我可能会执行per-filetype函数来为我进行设置。无耻地扯掉@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

当您想要进行更改并且更少切割和粘贴时,更改要少得多。