mager
mager

Reputation: 4873

Using vi, how can I remove all lines that contain [searchterm]?

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?

Upvotes: 0

Views: 1263

Answers (5)

EmFi
EmFi

Reputation: 23450

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\)

Upvotes: 5

retracile
retracile

Reputation: 12339

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.

Upvotes: 2

toolkit
toolkit

Reputation: 50257

Should just be

:g/color/d

Upvotes: 12

Joshua
Joshua

Reputation: 43317

Standard ex sequence:

  :/color/d

Upvotes: 2

Cfreak
Cfreak

Reputation: 19309

This did the trick for me:

:%s/.*color.*\n//

Upvotes: 0

Related Questions