Mike Morearty
22 Jan 2013

Fixing Vim’s indenting of HTML files

As Steve Losh pointed out, Vim’s default indentation for HTML has a pretty severe quirk:

The problem is that sometimes when I press return after a tag that I’ve already created Vim will unindent the new line and the previous line. This is excruciatingly annoying.

This was driving me crazy too. I was able to track down the issue.

The fix

Add this line to your ~/.vimrc:

autocmd FileType html setlocal indentkeys-=*<Return>

Once you do this, pressing the return key will only set the indentation on the newly created line. It will not touch the indentation of the previous line.

How it works

Vim’s indentkeys controls which keystrokes will cause Vim’s indentation logic to kick in. As the Vim help explains, if an asterisk appears before any keystroke in indentkeys, that means that Vim should reindent the line before inserting that key.

So in other words, having *<Return> in your indentkeys means, “When the user presses Return, reindent the current line and then create the new line.”

Doing indentkeys-=*<Return> removes that part of the indentkeys setting.

comments powered by Disqus