How do I get vim to indent all HTML tags?

Vic*_*tor 3 vim html

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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)"

fal*_*ner 9

确保安装了完整的 vim:

sudo apt-get install vim
Run Code Online (Sandbox Code Playgroud)

然后创建一个以~/.vimrc以下内容命名的文件:

filetype plugin indent on
Run Code Online (Sandbox Code Playgroud)

那么它应该工作。

UPDATE(更新源)

在 7.4 版本中,缩进文件发生了变化,这就是导致问题的原因。新的缩进不会缩进每个 html 标签,但您可以使用以下命令指定要缩进的标签:

:let g:html_indent_inctags = "html,body,head,tbody"
Run Code Online (Sandbox Code Playgroud)

在 vim 中,或者像往常一样没有:in ~/.vimrc。(当然,把那些标签放在你想要缩进的列表中。)

或者你可以换回旧版本的缩进文件来代替新版本。在 12.04 中,旧的缩进文件/usr/share/vim/vim73/indent/html.vim位于/usr/share/vim/vim74/indent/html.vim. 或者仅为您的用户覆盖它并使其免受更新的影响~/.vim/indent/html.vim