How to compute the number of times word appeared in a file or in some range

vehomzzz picture vehomzzz · Sep 9, 2009 · Viewed 14.9k times · Source

Sometimes I want to see how many times a certain function is called in a file or a code block. How do you do that? I am using Vim 7.2.

I presume you have to use !wc or some such.

Answer

dcruz picture dcruz · Sep 9, 2009

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

:%s/pattern//gn

The 'n' flag count the number of occurrences without doing any change to the document.

For counting the total number of words, you have several options.

If you want to run as an external command:

:!wc -w %

If you want to run it inside VIM:

:w !wc -w