In trying to get vim
to indent .html
files properly, I followed the examples set out here.
Given the following file index.html
As of 7.4.52
within vim:
:let g:html_indent_inctags = "html,body,head,tbody"
:call HtmlIndent_CheckUserSettings()
else in .vimrc:
let g:html_indent_inctags = "html,body,head,tbody"
I wanted to just to add this to a comment on the top answer, to give back, after spending too much time not getting the answer to work, but apparently don't have enough reputation :(
As mentioned in Cory's answer, the currently distributed version is Vimscript 2075. If you go to that plugin page you can see documented all the tags that by default will increase indent.
None of the tags you gave in your example are in this default list, but there are plenty of them.
Since indentation of HTML is very open to user preference, the plugin maintainer has included an option to add or remove tags to or from the list of tags that increases indent. See :help html-indent, where it suggests:
You can add further tags with:
:let g:html_indent_inctags = "html,body,head,tbody"
Between versions 7.3 and 7.4, the bundled html.vim
file located in $VIMRUNTIME/indent
changed. The currently distributed version is actually Vimscript #2075, and it doesn't indent some html
tags by default.
I recommend Ben's solution above, but alternatively you can revert to a previous version of the distributed html.vim
file.
To do this, just replace the existing 7.4 html.vim
file with the one from 7.3.
wget ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2
tar jxf vim-7.3.tar.bz2
cp vim73/runtime/indent/html.vim ~/.vim/indent/
The first troubleshooting step is to run :scriptnames
. If you don't see .../indent/html.vim
, then that means the plugin is not loaded correctly and indenting won't work properly. It will probably just left indent every line. (The problem is that vim doesn't give an error, so it seems like the indenting is doing an awful job.)
The most reliable way to get it working is to put this line in your ~/.vimrc
.
filetype plugin indent on
Then open the file with vim again, and run :scriptnames
. You should see .../indent/html.vim
now. Then type gg=G
to autoformat the entire file.
One important note that tripped me up for a while: If you don't put it in ~/.vimrc
and just type :filetype plugin indent on
after you've opened the file, you will have to re-open the file again with :e
. The indent plugin has to be loaded before you open the file. Run :scriptnames
to confirm.
Side note: you don't need to worry about smartindent
or autoindent
settings, those are for something else.