Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim variable length wildcard search and replace?

I'm trying to clean a Frontpage-generated html file, and there are a ton of tag attributes I need to delete, like:

style="font-size: 10.0pt; font-family: Trebuchet MS; color: blue"
style="color: blue; text-decoration: underline; text-underline: single"
style="color: blue; text-decoration: underline; text-underline: single"
style="font-family: Trebuchet MS"
style="font-size:10.0pt;"
style="color: navy"

I can delete a set number of wildcards with a simple . command:

:%s/ style="........"//g

But is there a way to make the . variable length in that substitute command, so that one command will delete every style attribute in the whole document?

PS - I've searched for frontpage cleaners and found a few, but not clear how reliable they are, so scripting it myself instead. Open to suggestions here though.

like image 442
Kurtosis Avatar asked Jan 18 '12 05:01

Kurtosis


1 Answers

This should eliminate all the style attributes in your HTML:

:%s/ style=".*"//g

Edit: Sam Brinck brings up a good point. My code was based on your example alone. This code would gobble up too much, say if there were other attributes following the style="..." attribute. A safer alternative may be:

:%s/ style="[^"]*"//g

which means - remove all characters after style=" that is NOT a double quote [^"] until the next double quote is encountered. Thanks Sam!

like image 140
Web User Avatar answered Sep 23 '22 05:09

Web User