Vim Tips Wiki
(Change <tt> to <code>, perhaps also minor tweak.)
(Fixing typo)
Tags: Visual edit apiedit
Line 34: Line 34:
   
 
<pre>
 
<pre>
:3s/pattern//gn
+
:%s/pattern//gn
 
</pre>
 
</pre>
   

Revision as of 08:23, 12 May 2015

Tip 860 Printable Monobook Previous Next

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


To count the number of matches of a pattern, you could enter a substitute command such as

:%s/pattern/foo/g

After viewing the count in the status line, press u to undo the change.

However, in recent versions of Vim, there is a better procedure. The following will display the count, but will not change the buffer.

:%s/pattern//gn

Similarly you can display the number of lines the pattern matches by omitting the g.

:%s/pattern//n

In newer versions of VIM, if you want to know the amount of matches of some lines, you may use the substitute command without the % such as:

:%s/pattern//gn

or

'<'>s/pattern//gn

Comments