How can I delete all lines that do not begin with certain characters?

mager picture mager · Nov 12, 2009 · Viewed 15.9k times · Source

I need to figure out a regular expression to delete all lines that do not begin with either "+" or "-".

I want to print a paper copy of a large diff file, but it shows 5 or so lines before and after the actual diff.

Answer

Marcin picture Marcin · Nov 12, 2009

In VIM:

:g!/^[+-]/d

Here is the English translation:

globally do something to all lines that do NOT! match the regular expression: start of line^ followed by either + or -, and that something to do is to delete those lines.