greyer
greyer

Reputation: 97

Why is vim not indenting two spaces for html?

So I am trying to set up my vimrc for python and web development. This is what my vimrc looks like.

"--------------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

However when I open or create an html file it indents 8 spaces rather than 2. What am I missing?

Thanks!

Upvotes: 3

Views: 1942

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172500

You can check where the indent options were set via

:verbose setlocal ts? sts? sw? et?

I believe in your case, the problem is with the list of patterns in your :autocmd:

au BufNewFile,BufRead *.js, *.html, *.css

There must not be a whitespace between the patterns:

au BufNewFile,BufRead *.js,*.html,*.css

See :help autocmd-patterns; it talks about comma-separated list, and the example there also doesn't have whitespace.

alternative approaches

By encoding the file patterns for the various languages, you're duplicating the built-in filetype detection.

I would recommend to put these :setlocal commands into ~/.vim/after/ftplugin/html.vim (and so on. This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/html.vim.)

Alternatively, you could define an :autocmd FileType {filetype\} ... directly in your ~/.vimrc. This would at least avoid the duplication of file patterns, but tends to become unwieldy once you have many customizations.

Upvotes: 7

Related Questions