24

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.

Community
  • 1
  • 1
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • I've had the same problem on Mac OS X and reverted to 7.3 for the time being. I'm sure there's something to find in the runtime files, though. – romainl Oct 11 '13 at 17:52
  • Ask on the vim_use or vim_dev mailing list. That is the proper channel for potential bugs. – glts Oct 11 '13 at 18:35
  • 2
    @glts I'm actually on IRC right now and we've narrowed it down to a change in the runtimes between 7.3 and 7.4. – Cory Klein Oct 11 '13 at 18:36
  • 1
    @romainl Thanks actually goes to you for leading me in that direction. I'm going to figure out what specific change breaks it and see if I can't submit that as a change. – Cory Klein Oct 11 '13 at 18:37
  • 1
    'smartindent' actually has exactly zero effect for HTML if you enable filetype-specific indenting with "filetype plugin indent on". And if you do end up running into a file where smartindent will apply, I think you'll be disappointed. "smart" indent...well...isn't – Ben Oct 13 '13 at 17:55
  • Have you fixed the problem? – shengy May 26 '15 at 21:32
  • @shengy Yes, see the two answers below. – Cory Klein May 27 '15 at 20:21
  • I've tried all the solutions here, but none work for me. When I do `gg=G`, it says "12 lines indented", but it just left indents every line. And I have `` and `` as well. When I ran `:scriptnames`, it listed 3 files: `1: /usr/share/vim/vim74/filetype.vim`, `2: /usr/share/vim/vim74/ftplugin.vim`, `3: /usr/share/vim/vim74/indent.vim`. I didn't see `html.vim`, should that be there? – wisbucky Jun 21 '18 at 23:11
  • Finally figured it out. I had to re-open the file with `:e` after setting `:filetype plugin indent on` – wisbucky Jun 21 '18 at 23:16

4 Answers4

25

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"
Ben
  • 8,725
  • 1
  • 30
  • 48
  • 10
    There is something to be said for reasonable defaults in every piece of software, and I think this is a particular example where reasonable defaults failed horribly. – ldog Oct 13 '13 at 17:57
  • 3
    I personally think that any indent within , , or is wasted. EVERY HTML document has those and they don't really add any structure.

    I agree should not be indented, because I normally use it in-line with the text rather than starting a new line. So it is MY opinion, that the specific example given, is correctly indented. If you add a div, or a section, or a list, that will indent things, because it adds structure.

    – Ben Oct 13 '13 at 18:03
  • @Ben Thanks for figuring this out. Being new to HTML development in vim, I didn't even consider that some tags would not be indented. – Cory Klein Oct 13 '13 at 23:04
  • How come I can't get this to work with the ` – bongbang Jun 12 '16 at 19:02
  • Because, inside a ` – Ben Jun 13 '16 at 13:25
14

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/
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • 1
    *"but I will chose "functioning" over "efficient" any day."* I fully agree. Thanks for being less lazy than me. – romainl Oct 12 '13 at 07:20
  • 2
    The maintainer of that script actually really cares and asks for complaints (see e.g. here: [vim-dev list](https://groups.google.com/d/msg/vim_dev/1qagiM_Dpyo/v0AheXEOX0wJ)), so in the spirit of Open Source you should tell him directly, so he can improve his indenting script – Christian Brabandt Oct 12 '13 at 11:09
  • That, and the script DOES work. It just lacks documentation in the help as to what the default tags are that will add to the indent. As I mention in my answer, the tags in the example given are NOT tags that will add to the indent by default. Just configure it to indent the specific tags you want and everything will work, probably BETTER than the 7.3 version. There are reasons this script was chosen in 7.4 to relpace the old version! – Ben Oct 12 '13 at 16:36
  • If you do this (replace the new version of the plugin with an old unmaintained version) then you'll never get any bugfixes or improvements ever again. Want any HTML5 support? Too bad. Want your bug reports to be dealt with? Too bad. How about either configuring the new plugin, or contacting the maintainer with specific requests instead? – Ben Oct 13 '13 at 17:52
  • 1
    @ChristianBrabandt I think maybe I will suggest to him to update the documentation on vim.org to clearly indicate that some basic html tags are not indented by default. This would have prevented me from mistakenly thinking it was broken. I think the text `:let g:html_indent_inctags = "html,body,head,tbody"` which is currently halfway down the page isn't quite communicative enough. – Cory Klein Oct 13 '13 at 23:08
7

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

milesvp
  • 126
  • 1
  • 4
0

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.

wisbucky
  • 33,218
  • 10
  • 150
  • 101