0

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?

2

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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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