Vim Tips Wiki
(Fix.)
Tag: sourceedit
(Tricks)
Tags: Visual edit apiedit
Line 32: Line 32:
   
 
==Comments==
 
==Comments==
  +
To count number of occurrences of a pattern under search,
  +
:%s///gn
  +
This makes it easier for words, once the cursor is on the desired word, doing '#' or '*' followed by this count is much easier than typing.

Revision as of 10:53, 18 May 2015

Tip 860 Printable Monobook Previous Next

created 2005 · complexity basic · author Marc Weber · version 7.0


To count the number of matches of a pattern, use the substitute command with the n flag. The following shows the number of times that pattern matches text in the current buffer:

:%s/pattern//gn

Omit g to display the number of lines where the pattern matches:

:%s/pattern//n

To restrict the count to a region of the text, specify a range instead of % (% means all lines). For example, the following counts the number of occurrences in lines 10 to 50 inclusive:

:10,50s/pattern//gn

The following counts the number of occurrences in the lines in the most recent visual selection.

:'<'>s/pattern//gn

Comments

To count number of occurrences of a pattern under search,

:%s///gn

This makes it easier for words, once the cursor is on the desired word, doing '#' or '*' followed by this count is much easier than typing.