Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know this works:

:%s/<string>//gn

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

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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 /

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.