indenting XML in vim

Sometimes I need to look inside XML files which consist of one big line. It’s very impossible, and reformatting takes a time. Fortunately, I’ve found a good solution for vim.

I added to my “.vimrc” file:

map <F2> <Esc>:1,$!xmllint --format -<CR>

Now I can press “F2” button, and vim indents the text.

More hints on indenting XML in vim: Tip #349: Format your xml document using xmllint.

18 Responses to “indenting XML in vim”

  1. Rick Says:

    Thanks. Quick hit on google and did what I needed.

  2. Lynn Says:

    Thanks, this works great on both linux and mac…

  3. mustafa Says:

    Thanx for the tip. really useful.

  4. Johan Says:

    Great! This was just what I needed, and with libxml2 from http://sourceforge.net/projects/gnuwin32/ it works fine on Windows too.

  5. pzy Says:

    It works great! Thanks.

  6. Sean Says:

    Great tip, thanks for that :)

  7. Luke Says:

    Great, thanks!

  8. Nachiket Says:

    Excellent..thanks!

  9. Angelo Says:

    cool, thanks

  10. Jan Wegner Says:

    Hi, plain great
    but what about errors (e.g. not wellformed, or extra chars …).
    When they occur. The buffer is overwritten w/ the
    errormessage and the original buffercontent is lost
    Or do I miss something.

    Cheers, Jan

  11. olpa Says:

    Yes, xml shouldn’t contain errors, otherwise no tool can re-format the document.

  12. Serdar Says:

    You can use eclipse and wtp plugin to re-format xml if it is not well formed.

  13. Praveen Says:

    Great, Thanks

  14. RazzleGames Says:

    Thanks, very Nice! We can do anything with vim!

  15. mvkbabu Says:

    awesome, 1st hit and found the answer !

  16. Marek Says:

    Thanks! First hit and very helpful

  17. Matt Says:

    Thx!

  18. Tom Haines Says:

    Wow….just…wow.

Leave a Reply