2
I want to find the number of times 'x' is present in my file, so I submit %s/x//gn
and get the correct answer.
How can I capture the resultant count into a variable using vimscript on the command-line?
The following solution was hinted at by an answer below:
:let cnt=0
:g/x/let cnt=cnt+1
:echo cnt
However, cnt
is made to store the total number of lines in the buffer that have an x
, not how many x
s there are in the whole file.
So, the original question still stands.
The link you have mentions the command :g, which can be used to count lines where a regex matches, but not the number of times it matches on a line, which is what I need.
– drapkin11 – 2011-03-30T18:19:52.037@drapkin11: Good point. I know it could be done with a Vim script, but I'm not aware of a more elegant way to do it. – Heptite – 2011-03-30T21:25:47.720