2015-04-07 41 views
3

我想在vi或vim中只删除注释中的“去”。你能告诉我怎么做?vi vim删除特殊块中的单词

/* 
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 

感谢。

+0

是否要删除注释块中的所有“go”?或者当他们单独坐在一条线上时,那些“走”出来?请解释为什么你的评论块中仍然有“预期的结果”。 – Kent

回答

2

你可以试试这个:

g/\/\*/.,/\*\//s/\<go\>//g 
  • g/<pattern>/ - 匹配包含a给定的图案,在这种情况下评论的开始
  • .,/<patter>/ - 从当前行执行以下ex命令到匹配的图案,在该示例中是评论
  • 的端
  • s/<pattern>//g下一行 - 替换所有在每行的空字符串上出现模式
3

这条线适用于你的例子:

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

一些项目可能要采取Vim的很大的帮助文档看看:

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

它还会删除句子本身中的“去”。我不认为这是OP的意图。接下来,我相信,这将更接近OP希望得到的东西,那就是“g/\ v \/\ \ * \ _。{ - } \ * \ //,/ \ v \ * \ // s/^ go $ \ n'。 –

+0

@LievenKeersmaekers我看到,在他的输出中,并非所有的“去”评论被删除.... – Kent

+0

非常感谢,特别是。获取帮助主题。 –