views:

219

answers:

6

I want to remove all lines in a CSS file that contain the word "color".

This would include:

body {background-color:#000;}
div {color:#fff;}

How would you do that using the :%s/ command?

A: 

This did the trick for me:

:%s/.*color.*\n//
Cfreak
+2  A: 

Standard ex sequence:

  :/color/d
Joshua
+10  A: 

Should just be

:g/color/d
toolkit
+2  A: 

And just to give you a completely different answer:

:%!grep -v color

:)

This alludes to a larger bit of functionality; you can apply your knowledge of *nix commandline filters to editing your code. Want a list of enums sorted alphabetically? Visual select, :!sort and it's done.

You can use uniq, tac, etc, etc.

retracile
+1 now that is useful!
Joshua
+1  A: 

I'm not sure if this will work for vi, but it works in vim:

:g/color/d

See :help global for more info.

Permanuno
+5  A: 

Is that such a wise idea? You could end up doing something you don't want if your CSS has sections like this

body {background-color: #000;
  font-size: large;
}

p {
  color: #fff; float: left;
  }

You're better off removing only the properties containing color

s/\(\w\|-\)*color\w*\s*:.\{-}\(;\|$\)//

Update: As too_much_php pointed out, the regex I didn't exactly work. I've fixed it, but it requires vim. It isn't feasible to forge a regex that only removes problem properties in vi. Because there are no character classes, you would have to do something like replacing the \w with \(a\|b\|c\|d\|....\Z\)

EmFi
But that regex doesn't work in vim! It doesn't even work in Perl.
too much php
Thanks for pointing it out. Fixed.
EmFi