3

I have a file which has the following structure

From: [email protected]
Subject: Bar
Date: 2017-06-27
Mailbox: /some/dire/ctory
<CR>

Such a block always contains of four lines followed by an empty line. I want to delete all blocks which contain Subject: Bar. In the past I recorded keystrokes and replayed them. So I searched for the string (/Subject: Bar), hit dap and replayed it. This took quite a while for larger files. Thatswhy I'm looking for another approach.

How can I delete blocks of text in vim when they contain a search pattern?

1
  • 1
    I think this question can be rephrased as "How can I delete all headers from an email or mbox file?" Jun 28, 2017 at 8:57

1 Answer 1

6

You can use a macro:

Once your search register is set to Subject: Bar you can empty a register (q in this example) and record the following macro:

qqq
qqndap@qq

The first line empties the q register, the second creates a macro like this:

qq           start recording
n            go to next match
dap          delete the paragraph
@q           call the macro recursively
q            stop recording

Then you simply need to play the macro once: @q

Or you could use a global command:

g/Subject: bar/norm! dap

Which means: for each line matching Subject: bar execute the ex command norm! dap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.