Searene
Searene

Reputation: 27594

search and delete multiple lines including the matched line

text file:

a
b
c
d
e
f

Now I want to search for the content c and delete the matched line and the another line immediately before it, that is, I want to delete the line b and c, I've tried :g/c/ .,-1 d, but it didn't work. How to make it?

Upvotes: 2

Views: 75

Answers (2)

Amadan
Amadan

Reputation: 198364

Good idea, bad syntax. :) This should do it:

:g/c/-1,.d

Upvotes: 6

Anurag Peshne
Anurag Peshne

Reputation: 1547

You can use replace:

:%s/.*\nc\n//gc

If there is some text after c, you can use:

:%s/.*\nc.*\n//gc  

Upvotes: 1

Related Questions