Search for string and get count in vi editor
THE way is
:%s/pattern//gn
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.
:%s/string/string/g will give the answer.
(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)
:g/xxxx/d
This will delete all the lines with pattern, and report how many deleted. Undo to get them back after.