How to delete all lines matching a pattern and a line after in Vim?

24,440

Solution 1

You can use

:g/word/normal 2dd

This finds all instances of word and then executes the command after it. In this case it executes 2dd in normal mode

Solution 2

Use :g[lobal] with a d[elete] command with a ,+1 range to delete matched line and the next:

:g/word/,+1d

Solution 3

You can use the :g[lobal] command:

:g/This line/norm 3dd

Solution 4

another way with :g

:g/word/norm dj

Solution 5

You may want to try this:

:g/your pattern go here/d
Share:
24,440
Boris Pavlović
Author by

Boris Pavlović

Interested in developing programming, concurrent model design skills, design patterns, refactoring, testing... Computer languages: java, javaScript, sql, fsp, erlang...

Updated on October 03, 2020

Comments

  • Boris Pavlović
    Boris Pavlović over 3 years

    There's a text

    Title of the text
    
    This line contains a word that is matched by the pattern and it should be deleted.
    Next line is supposed to be deleted as well.
    
    Loren ipsum dolorem...
    
    This line contains a word that is matched by the pattern and it should be deleted.
    And this one should be deleted
    
    The end of the article
    

    How to delete every pair of lines matching the first line, e.g. 'This line contains a word...' and the line after that one. Result would be:

    Title of the text
    
    Loren ipsum dolorem...
    
    The end of the article