How do I calculate the number from within vim?
To count the number of times any pattern occurs, use:
:%s/pattern//gn
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