简体   繁体   中英

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

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, eg '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

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

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

:g/word/,+1d

您可以使用:g[lobal]命令:

:g/This line/norm 3dd

另一种方式:g

:g/word/norm dj

你可能想试试这个:

:g/your pattern go here/d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM