Vim Tips Wiki
(Adding categories)
(→‎Comments: refine key map)
Tag: Visual edit
(19 intermediate revisions by 13 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=7.0
 
|rating=4/19
 
|rating=4/19
|category1=
+
|category1=Searching
 
|category2=
 
|category2=
 
}}
 
}}
To count the number of matches of a pattern, you could enter a substitute command such as
+
To count the number of matches of a pattern, use the [[Search and replace|substitute command]] with the <code>n</code> flag. The following shows the number of times that <code>pattern</code> matches text in the current buffer:
  +
<pre>
  +
:%s/pattern//gn
  +
</pre>
   
 
Omit <code>g</code> to display the number of lines where the pattern matches:
 
<pre>
 
<pre>
:%s/pattern/foo/g
+
:%s/pattern//n
 
</pre>
 
</pre>
   
  +
To restrict the count to a region of the text, specify a range instead of <code>%</code> (<code>%</code> means all lines). For example, the following counts the number of occurrences in lines 10 to 50 inclusive:
After viewing the count in the status line, press <tt>u</tt> to undo the change.
 
  +
<pre>
 
  +
:10,50s/pattern//gn
However, in recent versions of Vim, there is a better procedure. The following will display the count, but will not change the buffer.
 
  +
</pre>
   
  +
The following counts the number of occurrences in the lines in the most recent [[visual selection]].
 
<pre>
 
<pre>
:%s/pattern//gn
+
:'<,'>s/pattern//gn
 
</pre>
 
</pre>
   
  +
==Word under cursor==
Similarly you can display the number of lines the pattern matches by omitting the g.
 
  +
To count the number of occurrences of the last used search pattern, you can leave out the pattern entirely:
 
 
<pre>
 
<pre>
:%s/pattern//n
+
:%s///gn
 
</pre>
 
</pre>
  +
This makes it easy to count the number of occurrences of the word under the cursor: first press <code>*</code> to [[Searching|search for the current word]], then enter <code>:%s///gn</code> to count all occurrences of that word.
  +
  +
To access this quickly, define a shortcut command like
  +
map ,* *<C-O>:%s///gn<CR>
  +
then typing <code>,*</code> in quick succession will run the following: <code>*</code> finds the next match to the word under the cursor, <code><C-O></code> (CTRL+O) returns the cursor to where it started, then <code>:%s///gn</code> does the counting we want. Of course this also works with any choice of command instead of <code>,*</code>, and you can even overwrite the meaning of <code>*</code> with <code>nnoremap * *<C-O>:%s///gn<CR></code> (see <code>:help map</code>)
  +
 
==Comments==
 
==Comments==
  +
[[Category:Searching]]
 
  +
...you just became my personal hero...))
  +
  +
This thing is causing "Trailing characters" error in gvi editor....what might be the problem?
  +
:Which thing? One problem could be that "gvi" is not necessarily the same as "gvim" and may not actually be Vim, but something else, or a very stripped-down version. But I'm only speculating here without version information about "gvi" and without knowing which specific command gives that error. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 17:05, October 25, 2016 (UTC)
  +
Just to add one thing here, after running suggested command, cursor actually moves to first non space character in the line where search started, not at the beginning of searched pattern. For that add 'n' at the end to move cursor to beginning of the word, like <code>nnoremap * *<C-O>:%s///gn<CR>n</code>..-@Rakesh
  +
  +
Add <code>``</code> at the end to move cursor back to the beginning of word, which keeps the count status line.
  +
  +
Like: <code>map ,* *<C-O>:%s///gn<CR>``</code>
  +
  +
-@YingchunLi

Revision as of 00:44, 8 November 2017

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

Word under cursor

To count the number of occurrences of the last used search pattern, you can leave out the pattern entirely:

:%s///gn

This makes it easy to count the number of occurrences of the word under the cursor: first press * to search for the current word, then enter :%s///gn to count all occurrences of that word.

To access this quickly, define a shortcut command like

map ,* *<C-O>:%s///gn<CR>

then typing ,* in quick succession will run the following: * finds the next match to the word under the cursor, <C-O> (CTRL+O) returns the cursor to where it started, then :%s///gn does the counting we want. Of course this also works with any choice of command instead of ,*, and you can even overwrite the meaning of * with nnoremap * *<C-O>:%s///gn<CR> (see :help map)

Comments

...you just became my personal hero...))

This thing is causing "Trailing characters" error in gvi editor....what might be the problem?

Which thing? One problem could be that "gvi" is not necessarily the same as "gvim" and may not actually be Vim, but something else, or a very stripped-down version. But I'm only speculating here without version information about "gvi" and without knowing which specific command gives that error. --Fritzophrenic (talk) 17:05, October 25, 2016 (UTC)

Just to add one thing here, after running suggested command, cursor actually moves to first non space character in the line where search started, not at the beginning of searched pattern. For that add 'n' at the end to move cursor to beginning of the word, like nnoremap * *<C-O>:%s///gn<CR>n..-@Rakesh

Add `` at the end to move cursor back to the beginning of word, which keeps the count status line.

Like: map ,* *<C-O>:%s///gn<CR>``

-@YingchunLi