11

I know this works:

:%s/<string>//gn

But it seems a bit strange to use the substitution command to count, rather than global..

2 Answers 2

9

I've previously used a plugin for this. vim-indexed-search gives you a message in the command line such as:

2 of 32 matches

For searches using /

7

It seems there's no other way. Indeed, this is the official way described in the Vim documentation.

See this SO question titled: Counting occurrences in Vim without marking the buffer changed and by extension this paragraph from the Vim documentation.

Alternatively there's this SO question too, titled: Search for string and get count in vi editor, but note that this method will only count lines not actual matches, so if you have multiple matches per line, you'll not get a correct count.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .