Using Vim, how do you use a variable to store the number of patterns found?

This question was useful for getting an invoice of a specific template in Vim, but it would be useful for me to store the invoice and summarize the results so that I could echo a summary.

I teach a class in basic HTML for some high school students, and I use this script to quickly check the number of required elements on all of my pages without leaving Vim. It works great, but when students have more than 10 files .html, it becomes cumbersome to add several sections manually.

Sort of:

img_sum = :bufdo %s/<img>//gen

it would be nice. I think I will write a ruby ​​script to check the pages in more detail and check the structure, but for now I am wondering how to do this in Vim.

+5
source share
2 answers

The problem can be solved by the counter, separately from one built-in :substitute: use the Vim-script variable to hold the number of matches templates. A convenient way to register each match and change a specific variable, respectively, is to take advantage of the substitution using the expression function :substitute(see :help sub-replace-\=). The idea is to use a lookup that evaluates the increment of the expression counter in each case and does not change the text that it uses on.

Ex ( \= ), :let . gVim find/replace ", , , ( , -). map() , . map(), , .

, . , \ze \zs (. :help /\zs, :help /\ze). , . , , - . , .

.

:let n=[0] | bufdo %s/pattern\zs/\=map(n,'v:val+1')[1:]/ge
+7

, grep :

:let found=0
:bufdo let found=found+(system('grep "<p>" '.expand('%:p') . '| wc -l'))
:echo found
0

All Articles