【问题标题】:vi vim delete a word in a special blockvi vim 删除特殊块中的一个单词
【发布时间】:2015-06-11 23:33:31
【问题描述】:

我只想在 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”。

标签: regex vim vi


【解决方案1】:

此行适用于您的示例:

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

你可能想看看 vim 的帮助文档中的一些项目:

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

【讨论】:

  • 它还删除了句子本身中的“go”。我不认为这是OP的意图。我相信,跟随会更接近 OP 想要的 g/\v\/\*\_.{-}\*\//,/\v\*\//s/^go$\n
  • @LievenKeersmaekers 我看到,在他的输出中,并非所有评论中的“go”都被删除了......
  • 非常感谢,尤其是。帮助主题。
【解决方案2】:

你可以试试这个:

g/\/\*/.,/\*\//s/\<go\>//g
  • g/&lt;pattern&gt;/ - 匹配包含给定模式的行,在这种情况下是注释的开头
  • .,/&lt;patter&gt;/ - 从当前行到匹配模式的下一行执行以下 ex 命令,在本例中是注释的结尾
  • s/&lt;pattern&gt;//g - 用空字符串替换每行出现的所有模式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-29
    • 2011-10-14
    • 1970-01-01
    • 2012-05-30
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多