vimrc: 只对某些文件类型执行多行

vimrc: execute multiple lines only for certain filetypes

我的 .vimrc 中有几行我只想用于某些文件类型(HTML 在我的例子中)

目前看起来像

autocmd! FileType html set matchpairs+=<:>
autocmd! FileType html abb <div> <div><CR><\div><up><CR><TAB>
autocmd! FileType html abb <span> ...etc.

我在想,我是否真的需要每次都重新输入第一部分,或者类似的东西

autocmd! FileType html[
    set matchpairs+=<:>
    abb <div> <div><CR><\div><up><CR><TAB>
    abb <span> ...etc.
]

Vim 的“文件类型”机制已经很好地满足了您的需求,因此您不必用特定于文件类型的东西污染您的 vimrc

如果它不存在,创建$HOME/.vim/after/ftplugin/html.vim(在类Unix系统上)或$HOME/_vimfiles/after/ftplugin/html.vim(在Windows上)并输入以下内容:

setlocal matchpairs+=<:>
abbreviate <buffer> <div> <div><CR><\div><up><CR><TAB>
abbreviate <buffer> <span> ...etc.

注意……

  • 你不再需要自动命令了,
  • :help :setlocal 优于 :help :set,
  • <buffer> 被添加到您的映射和缩写中以防止它们泄漏到其他文件类型,请参阅 :help :map-<buffer>

:help filetype

使用 \ 进行续行,使用 | 分隔命令。

autocmd! FileType html
    \ set matchpairs+=<:>|
    \ abb <div> <div><CR><\div><up><CR><TAB>|
    \ abb <span> ...etc.

参见 :help :bar:help line-continuation