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 pasted a rather large chunk of XML into gVim (on Windows) to edit it. The tool the generates the XML does not put in any linebreaks so the XML pastes as a single line, which makes it rather hard to read/edit/verify. I tried the usual formatting trick gg=G, but this does nothing, probably because it's not bad formatting to have everything on a single line. Is there a quick and easy way to force the linebreaks and formatting?

share|improve this question

2 Answers 2

:s/>/\r>/g

then G=gg

share|improve this answer
    
Hmm not bad, but now the value of each element is on a new line with the closing tag. I was hoping for something like "<a>value</a>", but I get "<a> \r value</a>" –  FrustratedWithFormsDesigner Aug 3 '10 at 14:58
up vote 0 down vote accepted

Short of using xmllint, I found I could clean it up with 2 regular expressions:

:s/></>\r</g
:%s/<\(.*\)>\n\s*<\/\1>/<\1><\/\1>   

and THEN run gg=G. The purpose of the second regex is to fix things like:

<a>
</a>

and turn them into <a></a>

Seems to work OK and I can't find any lost/mangled data. I guess I could make that whole thing into a macro or something...

share|improve this answer

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.