0

It is possible to delete all lines matching a pattern using :g/pattern/d.

Using the following command, you can replace words matching a pattern, 20 lines after where the cursor currently is: :s/pattern/changed_pattern/g20

How do you delete all lines matching a pattern, but N lines after where the cursor currently is ?

I've tried : 34,50/pattern/d however it delete backward, and inconsistently. Beside, it requires you to know exactly the lines between which to apply the deletion.

2
  • 1
    Can you please provide a short example input with the expected output?
    – Quasímodo
    Commented Jul 17, 2020 at 20:00
  • What do you mean by "backward" and "inconsistently"? An example of what you're seeing and what you expected might help...
    – filbranden
    Commented Aug 16, 2020 at 19:58

1 Answer 1

1

These commands should do the trick

:global/pattern/.+1,.+Ndelete
:global/pattern/\/+1,\/+Ndelete

The key is to understand [range] for delete command.

  • .+1 one line after cursor line
  • .+N N lines after cursor line
  • \/+1 one line after current match
  • \/+N N lines after current match

See :help cmdline-ranges for more details.

Be aware it will delete pattern if it is in N lines range!

Your Answer

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

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