在特定块中,使用vi/vim删除一个单词。

3
我想在注释中使用vi或vim删除" go "。 请问如何操作?
/*
the following gos should be deleted: in the comment
go
*/

the following go should not be deleted
go


/*
the following go should be deleted: in the comment
go

and some more words
go
*/

the following go should not be deleted
go

删除操作的结果应如下所示:
/*
the following gos should be deleted: in the comment
*/

the following go should not be deleted
go


/*
the following go should be deleted: in the comment

and some more words
*/

the following go should not be deleted
go

谢谢。

你想要删除注释块中的所有"go"吗?还是只有那些单独占据一整行的"go"?请解释为什么在预期结果中仍然有"go"存在于你的注释块中。 - Kent
2个回答

3

这行代码适用于你的示例:

%s#/\*\zs\_.\{-}\ze\*/#\=substitute(submatch(0),'go','','g')#

在Vim的帮助文档中,有一些你可能想要查看的内容:

:h \zs
:h \ze
:h \_.
:h /star
:h :s\=
:h substitute(

1
它还会删除句子本身中的"go"。我不认为这是OP的意图。接下来,我相信以下内容更接近于OP想要的g/\v\/\*\_.{-}\*\//,/\v\*\//s/^go$\n - Lieven Keersmaekers
@LievenKeersmaekers 我看到,在他的输出中,并没有删除所有注释中的“go”... - Kent
非常感谢,特别是帮助主题的支持。 - William Chung

2
你可以尝试这个方法:
g/\/\*/.,/\*\//s/\<go\>//g
  • g/<pattern>/ - 匹配包含给定模式的行,例如这里是注释开始
  • .,/<patter>/ - 从当前行执行以下 ex 命令到匹配模式的下一行,这个例子中是注释结束
  • s/<pattern>//g - 将每行中所有出现的模式替换为空字符串

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