Quincy's answer is fine, but there's an exact way to do this which doesn't require editing the buffer:
:%s/pattern//ng
This will print a message like 3 matches on 2 lines
, and no changes will be made to your buffer.
The n
flag makes the :substitute
command print the number of matches instead of performing an actual substitution; the g
flag enables reporting of multiple matches per line.
Another thing that might be useful to your use case is to print all lines that match a pattern:
:global/pattern/print
which can be shortened to:
:g/pattern
This is one of the simplest uses of the :global
command (which is mind-bogglingly powerful). It will simply print out all of the lines that match pattern
, and then (if there is more thenthan one line) you press Enter
or type another command to make it go away.
A bit of trivia: This command is the origin of the name grep
, as it would commonly be described as g/re/p
, where re
stands for "regular expression".