Vim Tips Wiki
No edit summary
(Change <tt> to <code>, perhaps also minor tweak.)
(4 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
|previous=859
 
|previous=859
 
|next=861
 
|next=861
|created=January 22, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Marc Weber
 
|author=Marc Weber
 
|version=5.7
 
|version=5.7
 
|rating=4/19
 
|rating=4/19
|category1=
+
|category1=Searching
 
|category2=
 
|category2=
 
}}
 
}}
Line 17: Line 17:
 
</pre>
 
</pre>
   
After viewing the count in the status line, press <tt>u</tt> to undo the change.
+
After viewing the count in the status line, press <code>u</code> 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.
 
However, in recent versions of Vim, there is a better procedure. The following will display the count, but will not change the buffer.
Line 29: Line 29:
 
<pre>
 
<pre>
 
:%s/pattern//n
 
:%s/pattern//n
  +
</pre>
  +
  +
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:
  +
  +
<pre>
  +
:3s/pattern//gn
  +
</pre>
  +
  +
or
  +
  +
<pre>
  +
'<'>s/pattern//gn
 
</pre>
 
</pre>
 
==Comments==
 
==Comments==

Revision as of 05:52, 13 July 2012

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:

:3s/pattern//gn

or

'<'>s/pattern//gn

Comments