Delete all lines containing a pattern - Vim Tips Wiki:
The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.

For example, to delete all lines containing "profile" (remove the /d to show the lines that the command will delete):

:g/profile/d
No freaking way. I have grep in VIm? Where were you guys in the '60s when I needed you?


Other, related helpfulnesses:

Inserting line numbers from Line numbers in VIM's search & replace - St�vel.eu:

Now all that's left is to change the XXX to the appropriate number. Type :%s/XXX/\=line(".") and hit enter

Matching what's not in a regular expression from Search for lines not containing pattern and other helpful searches - Vim Tips Wiki:

This will find everything but the regular expression you have specified. For example, if we want to find all the lines not containing the word 'foo', simply do:

/^\(\(.*foo.*\)\@!.\)*$

Just yesterday, I posted a comment on SO agreeing with a guy who said, "If you're a programmer who edits a lot of text, then it's important to learn an A Serious Text Editor." Couldn't be more right.  Here's my 2¢ follow-up.

You had me at, "If you're a programmer who edits a lot of text, then it's important to learn an A Serious Text Editor." The worst part is that it seems impossible to learn two (much less more) inside and out, almost by definition. You're studying edge cases at some point, and the knowledge of how to accomplish Task X in one doesn't usually translate. And there's always things I do in "fringe" editors -- I still use JEdit for JTidy, Code2HTML, to search multiple files, eg. Your main is a roll of the dice, and there's no silver bullet, but yes, absolutely learn one and play with many. ;^)

Random addition: I'm behind a long post on why I think UTF-8 is really useful.  Before I actually walked myself into an ASCII corner, it seemed like an insane hack.  Now I think there's enough practical benefit it might be useful.

Labels: ,