Changing Vim behavior by file type using single `.vimrc` (without ftplugin)

后端 未结 2 1645
名媛妹妹
名媛妹妹 2021-01-06 15:04

How to change Vim behaviour using single .vimrc file (without ftplugin) in elegant way ?

What do I mean is ... if I run many commends for C/C++ file , e

2条回答
  •  日久生厌
    2021-01-06 15:24

    You can use the following:

    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
    

    This created grouping of commands depending on the type of file that is opened, which is specified in the autocmd section. You still need to specify autocmd or au before each one, but they are nicely grouped.

提交回复
热议问题