TransWikia.com

Delete N lines after cursor that contain a specific pattern

Vi and Vim Asked by user96649 on August 31, 2021

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.

One Answer

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!

Answered by gradzikb on August 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP