This question may have an obvious answer that I'm missing, but how can I easily tell how many of a character, in this case " , " are in a line, or a specified string using vimscript?
VimScript does not have a special character type. In fact, you always
count() for substrings (or items in list):
:echo count(getline("."), ",")
You can use this:
:echo len(split(getline('.'), ','))
That will get the content of the current line, split it on the
, (comma) to a list and count the number of items of the list.
Note: that the
count() function just relatively recently learned about counting items in strings, while this solution is also compatible with Vim 7.
len(split(getline('.'), ",", 1)) - 1as split produces more items than separators, and also to count commas at the start and the end of the line.– MattOct 23, 2019 at 13:01