Using vi, how to get the number of times a word or pattern occurs in a file

How do I calculate the number from within vim?

+2


source to share


2 answers


To count the number of times any pattern occurs, use:



:%s/pattern//gn

      

+9


source


The following will work with unmodifiable files and the result can be saved and used elsewhere in our scripts.



:let g:n = 0
:g/pattern/let g:n += 1
:echo g:n

      

+2


source







All Articles