3

I was using vim on Debian 7 to edit an HTML document. I started typing: <HTML>Return

After the initial return, the cursor was indented one level, so that as I typed <head>, and the rest of my document, it would show up like this:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Test page</title>
    </head>
    <body>
        <div>
            <p>This is a paragraph.</p>
        </div>
    </body>
</html>

The tag on each line was simply followed by a Return and no presses on the Tab or Space key, and vim formatted this as I expected. Also, when typing the initial </ of a closing tag, vim would unindent that line so as to match the level of the corresponding opening tag.

Then, typing the same document into vim on Ubuntu 13.10, as I'm typing the very same document in the same manner, I get the following results:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test page</title>
</head>
<body>
<div>
    <p>This is a paragraph.</p>
</div>
</body>
</html>

Notice that the indentation doesn't start until the <p> tag, and I suppose any of its subsequent sibling and child elements. It seems that later versions of the HTML indentation style have opted to not indent tags such as html, head, body, etc.(?), for legibility.

I don't like the fact that vim doesn't indent all tags. What can I do to change this?

I tried looking at the indentation files for the different versions, but I didn't manage to figure out if I could do anything there. pastebins:

I'm using the following versions of vim:

  • Debian 7: "Vi IMproved 7.3 (2010 Aug 15, compiled Feb 10 2013 02:27:59)"
  • Ubuntu 13.10: "Vi IMproved 7.4 (2013 Aug 10, compiled Aug 12 2013 00:28:05)"
  • The proper vim package is installed, i.e. I don't have the vim-tiny package that is installed by default on Ubuntu. I also have filetype plugin indent on in my .vimrc. – Victor Dec 19 '13 at 20:38
9

Make sure you have the full vim installed:

sudo apt-get install vim

Then make a file named ~/.vimrc with the following contents:

filetype plugin indent on

Then it should work.

UPDATE (source of the update)

In the 7.4 version the indentation file was changed, that is what causing problems. The new indentation doesn't indent every html tag, but you can specify which tags to indent by using:

:let g:html_indent_inctags = "html,body,head,tbody"

in vim, or without the : in ~/.vimrc as always. (Of course put those tags in the list which you want to be indented.)

Or you can just swap back the older version of indent file in place of the newer one. In 12.04 the old indent file is located in /usr/share/vim/vim73/indent/html.vim so I guess for the 7.4 version the file is located in /usr/share/vim/vim74/indent/html.vim. Or to override it only for your user and keep it safe from updates put it in ~/.vim/indent/html.vim

| improve this answer | |
  • Thanks, but this is already the case. I wish it were that simple, but you might be able to see in the pastebin I've provided that vim doesn't indent those tags in the later version. Somewhere along the line they were taken out of the global HTML indent file. – Victor Dec 19 '13 at 20:04
  • @Victor Oh. I didn't noticed that. My mistake I didn't read the question accurately. I updated my answer. – falconer Dec 19 '13 at 20:54
  • Thanks a lot! That works wonderfully, and is a great solution for me as I'd rather not touch system files. Thanks for finding that for me. I'll accept your answer with this update edit. – Victor Dec 19 '13 at 21:08
  • I'm trying to work out how to stop Vim from indenting any HTML tags. – Flimm Nov 2 '16 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.