I want to search for a string and find the number of occurrences in a file using the vi editor.
-
13The selected answer (`:g/xxxx/d`)is terrible, dangerous, and inferior to others on this page. Please unselect it. – Bruno Bronosky Jan 11 '17 at 16:55
-
1i agree and it only tells you 'lines deleted' not 'occurrences deleted' – user1709076 Jul 05 '17 at 19:11
8 Answers
You need the n
flag. To count words use:
:%s/\i\+/&/gn
and a particular word:
:%s/the/&/gn
See count-items
documentation section.
If you simply type in:
%s/pattern/pattern/g
then the status line will give you the number of matches in vi as well.

- 108,024
- 16
- 131
- 187
(similar as Gustavo said, but additionally: )
For any previously search, you can do simply:
:%s///gn
A pattern is not needed, because it is already in the search-register (@/
).
"%" - do s/
in the whole file
"g" - search global (with multiple hits in one line)
"n" - prevents any replacement of s/
-- nothing is deleted! nothing must be undone!
(see: :help s_flag
for more informations)
(This way, it works perfectly with "Search for visually selected text", as described in vim-wikia tip171)

- 480
- 7
- 12
Short answer:
:%s/string-to-be-searched//gn
For learning:
There are 3 modes in VI editor as below
:
you are entering fromCommand
toCommand-line
mode. Now, whatever you write after:
is on CLI(Command Line Interface)%s
specifies all lines. Specifying the range as%
means do substitution in the entire file. Syntax for all occurrences substitution is:%s/old-text/new-text/g
g
specifies all occurrences in the line. With theg
flag , you can make the whole line to be substituted. If thisg
flag is not used then only first occurrence in the line only will be substituted.n
specifies to output number of occurrences//
double slash represents omission ofreplacement text
. Because we just want to find.
Once got the number of occurrences, you can Press N
Key to see occurrences one-by-one.
For finding and counting in particular range of line number 1 to 10:
:1,10s/hello//gn
- Please note,
%
for whole file is repleaced by,
separated line numbers.
For finding and replacing in particular range of line number 1 to 10:
:1,10s/helo/hello/gn

- 7,064
- 2
- 47
- 61
:g/xxxx/d
This will delete all the lines with pattern, and report how many deleted. Undo to get them back after.

- 26,737
- 24
- 105
- 146

- 2,394
- 2
- 15
- 27
-
6Of course, he can just omit the "d" so he doesn't have to unto the operation. – Rook Apr 03 '09 at 21:02
-
15Note this only tells you how many lines - not how many occurences. I think dirk's is a better solution. – Apr 03 '09 at 21:06
-
4My solution below correctly counts multiple occurences within a line and there is nothing to undo. – Mohit Chakraborty Apr 03 '09 at 21:14
-
-
-
use
:%s/pattern/\0/g
when pattern string is too long and you don't like to type it all again.

- 1,267
- 2
- 15
- 28
I suggest doing:
- Search either with
*
to do a "bounded search" for what's under the cursor, or do a standard/pattern
search. - Use
:%s///gn
to get the number of occurrences. Or you can use:%s///n
to get the number of lines with occurrences.
** I really with I could find a plug-in that would giving messaging of "match N of N1 on N2 lines" with every search, but alas.
Note:
Don't be confused by the tricky wording of the output. The former command might give you something like 4 matches on 3 lines
where the latter might give you 3 matches on 3 lines
. While technically accurate, the latter is misleading and should say '3 lines match'. So, as you can see, there really is never any need to use the latter ('n' only) form. You get the same info, more clearly, and more by using the 'gn' form.

- 66,273
- 12
- 162
- 149