tags:

views:

2644

answers:

4

Well I want to search for a string and find number of occurrences in a file opened using Vi editor.

+4  A: 

:%s/string/string/g will give the answer.

Mohit Chakraborty
What a better answer. +5 if possible
ojblass
+7  A: 

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.

dirkgently
It looks like this answer is for Vim users and not for Vi :(
kadeshpa
Do you only have vi on your system? Which version?
dirkgently
Someone tagged your quesiton with vim in it... I removed it.
ojblass
+4  A: 

:g/xxxx/d

This will delete all the lines with pattern, and report how many deleted. Undo to get them back after.

Kevin Beck
Of course, he can just omit the "d" so he doesn't have to unto the operation.
ldigas
Note this only tells you how many lines - not how many occurences. I think dirk's is a better solution.
anon
My solution below correctly counts multiple occurences within a line and there is nothing to undo.
Mohit Chakraborty
A: 

THE way is

:%s/pattern//gn

Gustavo