Cory Klein
Cory Klein

Reputation: 55870

HTML indenting not working in compiled Vim 7.4, any ideas?

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.

Upvotes: 25

Views: 7256

Answers (4)

wisbucky
wisbucky

Reputation: 38023

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.

Upvotes: 0

milesvp
milesvp

Reputation: 136

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 :(

Upvotes: 7

Cory Klein
Cory Klein

Reputation: 55870

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/

Upvotes: 14

Ben
Ben

Reputation: 8905

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"

Upvotes: 25

Related Questions