-
Tidy Up Your XML in Vim
Posted on June 13th, 2009 1 commentYou have some nice, compact XML, the kind without whitespace or newlines, like this:

Not easy to read. OK for computers to crunch–but if you want to get an idea of the document’s structure at a glance or do some editing–well, you might want it formatted a little nicer.
So let’s use Tidy to clean it up–all from the comfort of your Vim session…
(You could also use xmllint–your choice–depends on what you have installed and what you prefer.)
You can execute a shell command from Vim by typing:
:!Then to tidy up your XML run tidy with the following options:
tidy -mi -xml -utf8“-im” means indent and modify the original file; “-xml” means, you are dealing with XML and “-utf8″ is optional, it means “use UTF-8 for both input and output” so you’ll get readable characters instead of encodings if you have umlats or grave symbols.
You can see all of tidy’s formatting, encoding and other options by typing
tidy -hSo the whole command we type into Vim is:
:! tidy -mi -xml -utf8
You will drop to shell while the command executes:

Hit “return” when you are done and then Vim will ask if you want to reload the file:

And there you go, beautifully formatted HTML to read!

Enjoy
!


