vehomzzz
vehomzzz

Reputation: 44598

Using vi how to obtain a number of time a word or pattern occur in the file

How do calculate the number from inside the vim?

Upvotes: 2

Views: 311

Answers (2)

vehomzzz
vehomzzz

Reputation: 44598

For counting the number of times some pattern occurs, use:

:%s/pattern//gn

Upvotes: 9

Luc Hermitte
Luc Hermitte

Reputation: 32966

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

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

Upvotes: 2

Related Questions