Is there any way to store the number of matches in a variable in a VIMScript function?For instance I'm using:%s/,//gn
Store the number of matches in VimScript function?
vimscript;regular expression
I know that @romainl debugged the question as a XY problem in the comments but I guess it still could be useful to some people, so here is a solution. (It is deeply inspired from this answer)You can use this function (to add to your .vimrc or to your script):function! Count( word )  redir => cnt    silent exe '%s/' . a:word . '//gn'  redir END  let res = strpart(cnt, 0, stridx(cnt,  ))  return resendfunctionYou can call the function like this: :call Count(pattern).For example in this file:a,bcakj,dhjlkdfa,oiua ,lkjoiua, lkjoiua , lkji,With :echo Count(,) you'll get 7.