为了知道当前缓冲区中存在模式的次数,我做:
:%s/pattern-here/pattern-here/g
Run Code Online (Sandbox Code Playgroud)
它给出了模式的出现次数,但显然很麻烦,并且还具有设置"已更改"状态的副作用.
是否有更优雅的方式来计算?
Bru*_*ine 165
要避免替换,请将第二个模式留空,并添加"n"标志:
:%s/pattern-here//gn
Run Code Online (Sandbox Code Playgroud)
这被描述为官方提示.
:help count-items
Run Code Online (Sandbox Code Playgroud)
在VIM 6.3中,这是你如何做到的.
:set report=0
:%s/your_word/&/g # returns the count without substitution
Run Code Online (Sandbox Code Playgroud)
在VIM 7.2中,您可以这样做:
:%s/your_word/&/gn # returns the count, n flag avoids substitution
Run Code Online (Sandbox Code Playgroud)
:!cat %| grep -c "pattern"
Run Code Online (Sandbox Code Playgroud)
它不完全是vim命令,但它会从vim中为您提供所需的内容.
如果需要经常使用它,可以将其映射到命令.