Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting occurrences in Vim without marking the buffer changed

Tags:

vim

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?

like image 863
Paul Oyster Avatar asked Sep 16 '08 08:09

Paul Oyster


People also ask

How do I count occurrences in vim?

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.

How do I count specific words in Vim?

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.


1 Answers

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.

like image 100
Bruno De Fraine Avatar answered Sep 18 '22 15:09

Bruno De Fraine