wyc
wyc

Reputation: 55283

Remove <span style> tags around text with Vim

Basically, I would like to turn stuff like this:

<span style="font-size: 1.1rem;">Lorem ipsum</span>

into this:

Lorem ipsum

In a whole document.

What's the fastest way of doing this with Vim?

Upvotes: 0

Views: 438

Answers (5)

Dmitry Zagorulkin
Dmitry Zagorulkin

Reputation: 8548

you could use vim surround plugin by Tim Pope. it's very suitable for your needs.

Surround.vim is all about "surroundings": parentheses, brackets, quotes, XML tags, and more. The plugin provides mappings to easily delete, change and add such surroundings in pairs.

http://github.com/tpope/vim-surround

Upvotes: 2

Ikar Pohorsk&#253;
Ikar Pohorsk&#253;

Reputation: 5039

To achieve the same from the normal mode you might:

place the cursor somewhere between first and last bracket of the paired tags and copy what's inside the tag into register let's say p:

"pyit

then delete the whole pair tag:

dat

and then paste what's in the p register:

"pp

The advantage of this approach is that it would work for all pair tags. When recorded as macro you may get rid of many different tags by just finding the tag and running the macro.

Upvotes: 1

Cyrille
Cyrille

Reputation: 25144

Something along the lines of

:%s/<span style="[^"]*">\([^<]*\)<\/span>/\1/g

should do the trick. I'm not quite sure if you'd need to escape the capturing parentheses or not, though.

(Thanks Birei for the escaping)

Upvotes: 2

Kent
Kent

Reputation: 195169

if you want to remove all <span ..> tags, and leave the content, you could:

:%s/<span[^>]*>\([^<]*\)<.*/\1/

if you just want to do the substitution on those <span.. with certain style="..", you can just copy the <span style="font-size: 1.1rem;"> put it before \([^<]*\).....

if your file is xml format, and your vim was armed with xml plugin, you could do it with macro(or :g), with xml plugin, <localleader>d deletes the surrounding tags and leave the text value.

Upvotes: 3

Leoric80
Leoric80

Reputation: 27

:%s/<span style="font-size: 1.1rem;">Lorem ipsum<\/span>/Lorem ipsum/g

Thanks

Upvotes: -2

Related Questions