In order to know how many times a pattern exists in current buffer, I do:
:%s/pattern-here/pattern-here/g
It gives the number of occurrences of the pattern, but is obviously cumbersome and also has the side-effect of setting the 'changed' status.
Is there a more elegant way to count?
This makes it easy to count the number of occurrences of the word under the cursor: first press * to search for the current word, then enter :%s///gn to count all occurrences of that word.
Counting number of words To count the number of words in the current buffer, press g then Ctrl-g. To count the number of words in a block, select the block (for example, type V5j to select 6 lines) and again press g then Ctrl-g.
To avoid the substitution, leave the second pattern empty, and add the “n” flag:
:%s/pattern-here//gn
This is described as an official tip.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With