如何在Vim中删除所有与模式匹配的行和其后一行?

38

这里有一段文字

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

如何删除匹配第一行(例如“This line contains a word…”)和其后一行的每对行。结果应为:

Title of the text

Loren ipsum dolorem...

The end of the article
5个回答

66
您可以使用。
:g/word/normal 2dd

这会找到所有单词 "word" 的出现并在其后执行命令。 在本例中,它在正常模式下执行2dd


33
使用 :g[lobal] 命令与 d[elete] 命令配合使用,并指定范围为 ,+1,可以删除匹配的行及其下一行:
:g/word/,+1d

如果您想删除匹配行及其前一行,则可以使用以下变体::g/pattern/-1n,.d - Dmytro

6
您可以使用:g[lobal]命令:
:g/This line/norm 3dd

3

使用:g的另一种方法

:g/word/norm dj

2
你可以尝试这个方法:
:g/your pattern go here/d

虽然这行代码可能回答了问题,但最好能够提供一些解释为什么它这样做。 - David
1
这个命令只会删除匹配的行,而不是它后面的行。因此,它并不能回答这个问题。 - Dmytro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接