9

I try to get autoindent to work in VIM with html files, but unfortunately it won't work. Autoindent works perfectly with other languages, but it just won't work with html. I've already tried setting the filetype to html, as suggested in this question, but unfortunately it doesn't work either. Also, when I select everything and press = it doesn't indent as well, even though vim reports XX lines indented.

My .vimrc file looks like this:

set autoindent 
set cindent 
set tabstop=4 
set shiftwidth=2 
set expandtab

Any suggestions?

Community
  • 1
  • 1
Tiddo
  • 6,331
  • 6
  • 52
  • 85
  • I tried this, but with an already nicely formatted HTML file. Selecting everything and pressing `=` actually caused everything to shift to the left-most column (as in removed all indenting). Seems like it had the opposite effect of indenting everything. – Jason Down Apr 18 '12 at 17:42
  • That's exactly what happens with me too on a formatted HTML file. However the SO question I linked to suggested this. – Tiddo Apr 18 '12 at 17:47

1 Answers1

20

You have to add the following lines to your .vimrc to make sure vim uses file-specific indentions:

filetype on
filetype plugin on
filetype indent on

This way it will indent html as html when the filetype is set to html.

BergmannF
  • 9,727
  • 3
  • 37
  • 37
  • 13
    You can actually write that as "filetype plugin indent on", all in one line :) – Rook Apr 18 '12 at 18:22
  • 1
    Nice to know - might artificially shorten my `.vimrc` :) – BergmannF Apr 18 '12 at 18:26
  • 3
    If for some reason the above didn't work you may want to check if indentation instructions were loaded. In normal mode in `vi` type `:scriptnames` to see scripts loaded. You should find one `indent.vim` which holds instructions on how to load further indentation instructions depending on the name of the file to load to a buffer and its filetype. – valid Dec 14 '13 at 06:09
  • nice! I tried some of the other answers, but this is what I needed – Jimmy Long Jan 24 '23 at 15:04