Skip to main content
2 of 2
edited tags
Martin Tournoij
  • 63.3k
  • 26
  • 199
  • 274

How do I remove newlines inside arbitrary xml tags matching a pattern?

I'm working with large xml files that have tags, and some are spread across multiple lines, and some are on a single line. I'd like them to all be on a single line. There are other tags in the files besides the one I want to modify.

<group value="abc">
   <line value="abc" value2="def"/>
   <line value="ghi"
      value2="jkl"/>
</group>

I would like it to look like:

<group value="abc">
   <line value="abc" value2="def"/>
   <line value="ghi" value2="jkl"/>
</group>

I've tried two approaches so far. The first is to try to apply a join to a range for the <line> tag. The second is to try to do a substitution across multiple lines to remove newlines and extra whitespace. Some examples that almost work.

:g/<line/,/\/>/join
:v/\/>/g/<line/,/\/>/join
:g/<line\_.{-}\/>/s/\n\s*/ /g
JHiant
  • 123
  • 3