2013-04-25 87 views
20

有一個文本如何在Vim中刪除所有匹配模式和行的行?

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 

如何刪除每對匹配的第一行線,例如中'這一行包含一個單詞...'和之後的一行。結果將是:

Title of the text 

Loren ipsum dolorem... 

The end of the article 

回答

37

您可以使用

:g/word/normal 2dd 

此找到的單詞的所有實例,然後後執行命令。在這種情況下,在正常模式下執行2dd

20

使用:g[lobal]d[elete]命令與,+1要刪除的匹配線和未來:

:g/word/,+1d 
+0

另一個變體,如果你想刪除它之前的匹配行和行,然後使用這個:':g/pattern/-1n,.d' – Dmytro 2017-01-20 02:37:23

4

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

​​
2

另一種方式與:摹

:g/word/norm dj 
-3

你可以試試這個:

:g/your pattern go here/d 
+0

儘管這行代碼可能會回答這個問題,但最好能提供一些解釋爲什麼它會這樣做。 – David 2015-03-20 09:11:26

+0

該命令僅刪除匹配行,但不刪除匹配行。所以,它不回答這個問題。 – Dmytro 2017-01-20 02:34:57