在Vim中包含一个或多个关键字的行的操作

时间:2015-01-05 23:36:17

标签: vim

如果我希望在包含某个关键字的所有行上运行相同的命令,我可以使用全局命令,或者对包含该关键字的行 使用vglobal命令。

例如,如果我有一个包含以下内容的文本文件:

hello world
testing with foo
another test with bar in it
another foo line
last test line

:g/foo/d删除包含单词foo的所有行:

hello world
another test with bar in it
last test line

我可以使用此命令操作包含多个单词中的一个或多个的行; OR语句的某些内容。例如,删除包含单词foobar的所有行,并给我:

hello world
last test line

由于

2 个答案:

答案 0 :(得分:6)

全局命令将正则表达式作为输入。因此,您只需要对\|使用替换(or)。

:g/foo\|bar/d

会删除包含“foo”或“bar”的行。

答案 1 :(得分:4)

是的,您可以在正则表达式中使用\|运算符。所以:

:g/foo\|bar/d