In trying to get vim
to indent .html
files properly, I followed the examples set out here.
Given the following file index.html
:
<html>
<body>
<p>
text
</p>
</body>
</html>
I tried opening it like so (ignoring my .vimrc
to make sure it isn't interfering negatively)
vim -u NONE index.html
Then I set the options to enable automatic indenting:
:filetype plugin indent on
:set filetype=html # abbrev - :set ft=html
:set smartindent # abbrev - :set si
And then I indented the entire file with gg=G
, and this is the result:
<html>
<body>
<p>
text
</p>
</body>
</html>
I checked to make sure that the html.vim
file existed, and it's definitely there
$ head -2 ~/.vim/after/ftplugin/html.vim
" Vim syntax file
" Language: HTML
$ head -2 ~/.vim/ftplugin/html.vim
" Vim syntax file
" Language: HTML
My version of vim is 7.4:
$ vim --version | head -1
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Sep 23 2013 16:12:20)
And it includes smart indent:
$ vim --version | grep smartindent
-ebcdic +mouse +smartindent +xim
I'm at a loss as to why the indentation isn't working! Any clues or ideas to research this problem further?
FWIW, I'm running Ubuntu 13.04.
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/
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 :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With