HTML indent settings in Vim

Despite not agree­ing with all of it, “Just Use Sub­lime Text” (an invec­tive against Vim — or, more accu­rate­ly, against rec­om­mend­ing Vim to any­one who isn’t already indoc­tri­nat­ed by it) by Andrew Ray is an inter­est­ing read. The sec­tion deal­ing with indent­ing in par­tic­u­lar struck a chord with me:

Paste this into an emp­ty buffer:


:set ft=html and then gg=G. Let me know what you get. In all seri­ous­ness, nev­er, ever tell me what you get.

As I said in a tweet, what I got did­n’t offend me too much. But many peo­ple would argue that the <span> tag should be indent­ed inside the <p> tag. I’d prob­a­bly do this myself, actu­al­ly — and there have been aspects of Vim’s indent­ing that irk me. So I set about find­ing a solu­tion.

My first attempt was through oth­ree’s html5.vim plu­g­in. Out of the box, how­ev­er, this result­ed in some tru­ly weird indent­ing behav­ior:

I don’t know why this behaves this way, or whether it can be fixed, but I did­n’t plan to find out. Vim plu­g­ins need to impress me pret­ty quick­ly before I decide to invest time in them.

Final­ly, after some Google search­ing, I stum­bled across an answer on Stack Over­flow. Vim’s native HTML indent file allows for adding or remov­ing html tags from a list of those tags whose chil­dren ought to be indent­ed. In the exam­ple giv­en by Ray, a line break after a <p> did­n’t result in an increased indent. The solu­tion is to add the p tag to this list:

:let g:html_indent_tags .= '\|p'

This can be done man­u­al­ly after the html file­type is set, or can be set uni­ver­sal­ly by plac­ing that line (sans the ini­tial colon) in ~/.vim/after/indent/html.vim. Despite the appar­ent obscu­ri­ty of this option, it proves to be pret­ty pop­u­lar in peo­ple’s dot­files on GitHub.

The shipped html indent file was last updat­ed in June, 2013, and still does­n’t include p as an “indent­ing” tag. It seems to me that this is inten­tion­al, rather than a “bug” in Vim’s html indent­ing. Some­body has made the choice not to include p among that list, which, while you may dis­agree with it, is a valid pref­er­ence.