XML Beautifier for vim?

8

5

Is there a vim command or plugin that quickly "beautifies" XML?

I have a file that looks like

<parent><child attr="bla"><subitem>Hello!
</subitem></child>
</parent>

and I want to make it look like

<parent>
  <child attr="bla">
    <subitem>Hello!
    </subitem>
  <child>
<parent>

Michael Stum

Posted 2010-07-23T23:37:54.280

Reputation: 3 061

Answers

10

Using only vim builtins:

First replace all >< with > [newline] < :

:%s/></>\r</g

Then to reindent:

gg=G

These steps give me the right output for your example data.

redacted

Posted 2010-07-23T23:37:54.280

Reputation: 2 400

1Not sure why but the gg=G is not working for me. I also tried it on a small bock but the = command is not working. – sixtyfootersdude – 2011-03-31T14:27:03.480

Does == work on a single line? Maybe try :set ft=xml to set indent rules. – redacted – 2011-04-01T15:21:42.760