Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm aware of tidy for re-indenting an entire HTML page, but I work with templates and often want to re-indent just parts of an HTML document from vim. I can't see an example of how to do this natively with vim and I don't see how I can do this with tidy, since it tries to insert "missing" head tags etc. All I want is tags to be re-indented. Could someone point me towards a command line utility which re-indents HTML snippets so that I can tweak my .vimrc to use it?

share|improve this question

migrated from Apr 15 '13 at 5:00

This question came from our site for professional and enthusiast programmers.

up vote 2 down vote accepted

To re-indent parts of an HTML document, select them in visual mode and use the = command:

={motion}    Filter {motion} lines through the external program
             given with the 'equalprg' option.

To re-indent the whole file you can type gg=G (go to first line, filter until the last line).

share|improve this answer
In e.g. PHP files Vim is also often confused about which parts are PHP and which are HTML. As in this case, it might be needed to explicitly set the filetype to HTML for Vim to understand and use its HTML indenting rules, so: 1. :set ft=html, 2. V for visual line select and 3. = for re-indenting should work no matter how Vim originally identified the file. :set ft can also be used to generally switch between auto-indentation rules for different languages combined in a single file, which often is the case with templating languages. See <>; – Daniel Andersson Apr 15 '13 at 9:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .