tags:

views:

1106

answers:

3

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?

+6  A: 

Add the 'n' flag to avoid the substitution.

This is described as an official tip.

Bruno De Fraine
even more cumbersome, but at least it is now official that there is no elegant way...
Paul Oyster
If you've already performed a search using /, it becomes just :%s///gn
Peter Gibson
+2  A: 

:!cat %| grep -c "pattern"

It's not exactly vim command, but it will give you what you need from vim.
You can map it to the command if you need to use it frequently.

Ilya
A: 

This vimscript displays search with "At match #N out of M matches".

redacted