wmitchell
wmitchell

Reputation: 5745

vim removing xml tags

I have a one liner in vim which I regularly use for find and replace.

I now want to use it to remove tags - something like this but I looks like I need to escape the / I'm not sure what am I missing.

:%s~<Validator>*</Validator>~~g

Upvotes: 3

Views: 2535

Answers (3)

wittich
wittich

Reputation: 2311

Just topple over that subject and had trouble to remove content over multiple lines. Here is what I come up with:

Input:

<Validator>
    <!-- hello world -->
</Validator>

Vim search replace:

%s~<Validator>\_.\{-}<\/Validator>~~g

The following vim wiki help me find the right syntax: https://vim.fandom.com/wiki/Search_across_multiple_lines

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 342609

use awk.

awk -vRS="</Validator>"  '{gsub(/<Validator>.*/,"") }1' file

The above removes from tag to tag, even if they span multiple lines.

Upvotes: -2

wmitchell
wmitchell

Reputation: 5745

:%s~<Validator>.*</Validator>~~g

Does the trick

Upvotes: 6

Related Questions