[英]vim find report AND store number of occurrences in buffer
i get this part: s/pattern//gn using vim to report number of occurrences of "pattern". 我得到这部分:s / pattern // gn使用vim报告“模式”的出现次数。
but is there some way to store that reported number in a buffer for later use? 但是有什么方法可以将报告的号码存储在缓冲区中以备后用?
First of all, I guess what you meant "buffer" is "register/variable". 首先,我想您的意思是“缓冲区”是“寄存器/变量”。
There are different ways to achieve that: 有多种方法可以实现:
external command is most direct way, IMO 外部命令是最直接的方式,IMO
let a=system('grep -c "yourPattern" '.expand("%:p"))
Thus, variable a
contains the count. 因此,变量
a
包含计数。 But you need have grep
available, and the vim buffer should be a file. 但是您需要有
grep
可用,并且vim缓冲区应该是一个文件。
With searchpos()
function and loop. 使用
searchpos()
函数并循环。 From col1 line1, calling searchpos()
function, accumulate the matches, till the end of the file/buffer. 从col1第1行,调用
searchpos()
函数,累积匹配项,直到文件/缓冲区结束。 You need implement the loop. 您需要实现循环。
with redirect ( :redir
) to redirect the message to register, then do further processing. 使用redirect(
:redir
)将消息重定向到注册,然后进行进一步处理。 Thus you can have x matches in y lines
message in a register and pick the x
out later. 因此,您可以在寄存器
x matches in y lines
消息中有x matches in y lines
然后稍后再选择x
个。
I would prefer the first way. 我更喜欢第一种方式。
:h item
will show you the help doc for the item (function usage/option/command etc), do this if you didn't understand some command/function. :h item
会向您显示该项目的帮助文档(功能用法/选项/命令等),如果您不了解某些命令/功能,请执行此操作。
But first you should do :h window
, to know what is a buffer/window in vim. 但是首先您应该执行
:h window
,以了解什么是vim中的缓冲区/窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.