(restore comments section and clean up new section) Tag: sourceedit |
(→Word under cursor: add tip to make a shortcut mapping) Tags: Visual edit apiedit |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 28: | Line 28: | ||
The following counts the number of occurrences in the lines in the most recent [[visual selection]]. |
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== |
==Word under cursor== |
||
− | To count number of occurrences of |
+ | To count the number of occurrences of the last used search pattern, you can leave out the pattern entirely: |
+ | <pre> |
||
− | + | :%s///gn |
|
− | This makes it easier for single words; once the cursor is on the desired word, doing '#' or '*' followed by this method is much easier than typing the entire search pattern. |
||
+ | </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== |
Revision as of 16:03, 16 February 2016
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
)