Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 694
mager Avatar asked Nov 12 '09 20:11

mager


People also ask

How do I delete a line not matching in Vim?

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.


1 Answers

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.

like image 143
Marcin Avatar answered Oct 05 '22 11:10

Marcin