Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com Apr 15 '13 at 5:00

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

add comment

1 Answer

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
1  
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 <superuser.com/q/189982/49184>; –  Daniel Andersson Apr 15 '13 at 9:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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