October 2012
346 pages
7h 40m
English
| Tip 82 | Count the Matches for the Current Pattern |
This quick tip shows how to count the number of matches for a pattern.
There’s no way of making the search command report the number of matches in the current document. But here’s a command that does:
| => | :%s///gn |
| <= | 117 matches on 65 lines |
We’re actually calling the :substitute command, but the n flag suppresses the usual behavior. Instead of replacing each match with the target, it simply counts the number of matches and then echoes the result below the command line. By leaving the search field blank, we instruct Vim to use the current search pattern. The replacement field is ignored anyway (because of the n flag), so we can leave it blank too.
Whatever you do, don’t omit any of the ...
Read now
Unlock full access