为什么 vim 不为 html 缩进两个空格?

gre*_*yer 3 html vim

所以我正在尝试为 python 和 web 开发设置我的 vimrc。这就是我的 vimrc 的样子。

"--------------vundle------------------------
set nocompatible
filetype off

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

"add plugins here
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-surround'
Plugin 'itchyny/lightline.vim'

call vundle#end()
filetype plugin indent on
"------------------end of vundle-------------------

"--------------python development-----------------
"PEP8 python indentation and formatting
au BufNewFile,BufREad *.py
    \ set tabstop=4
    \ set softtabstop=4
    \ set shiftwidth=4
    \ set textwidth=79
    \ set expandtab
    \ set autoindent
    \set fileformat=unix

let python_highlight_all=1
syntax on

"---------------web development------------------
"basic tab spacing for html, css and js
au BufNewFile,BufRead *.js, *.html, *.css
    \ set tabstop=2
    \set softtabstop=2
    \set shiftwidth=2
Run Code Online (Sandbox Code Playgroud)

但是,当我打开或创建一个 html 文件时,它会缩进 8 个空格而不是 2 个。我错过了什么?

谢谢!

Ing*_*kat 7

您可以通过以下方式检查缩进选项的设置位置

:verbose setlocal ts? sts? sw? et?
Run Code Online (Sandbox Code Playgroud)

我相信在您的情况下,问题在于您的模式列表:autocmd

au BufNewFile,BufRead *.js, *.html, *.css
Run Code Online (Sandbox Code Playgroud)

模式之间不能有空格:

au BufNewFile,BufRead *.js,*.html,*.css
Run Code Online (Sandbox Code Playgroud)

:help autocmd-patterns;它谈论逗号分隔的 list,并且那里的示例也没有空格。

替代方法

通过对各种语言的文件模式进行编码,您可以复制内置的文件类型检测

我建议将这些:setlocal命令放入~/.vim/after/ftplugin/html.vim(等等。这要求您拥有:filetype plugin on; 使用after目录允许您覆盖由$VIMRUNTIME/ftplugin/html.vim.完成的任何默认文件类型设置。)

或者,您可以:autocmd FileType {filetype\} ...直接在您的~/.vimrc. 这至少可以避免文件模式的重复,但是一旦您进行了许多自定义,就会变得笨拙。