简体   繁体   中英

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".

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

     let a=system('grep -c "yourPattern" '.expand("%:p")) 

    Thus, variable a contains the count. But you need have grep available, and the vim buffer should be a file.

  • With searchpos() function and loop. From col1 line1, calling searchpos() function, accumulate the matches, till the end of the file/buffer. You need implement the loop.

  • with redirect ( :redir ) to redirect the message to register, then do further processing. Thus you can have x matches in y lines message in a register and pick the x out later.

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.

But first you should do :h window , to know what is a buffer/window in vim.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM