Basically title, the length of unicode characters as calculated by the len() function is off by one, for example

:echo len("à")


:echo len("°")

is there a way to fix this, maybe by setting some options?

  • There’s lots of resources that discuss the complications of counting length of unicode strings, but I can’t find one. It matters how you count: bytes? “Characters”? But some characters are combining and you don’t see them as you might think... etc.
    – D. Ben Knoble
    Aug 14 '20 at 23:40

Funny, I'm used to using len() to count items in a list but it turns out it's valid to pass a string, too, in which case the function returns length in bytes. I would use strlen() for that but either one works per documentation for len():

When {expr} is a String or a Number the length in bytes is used, as with strlen().

Anyways, it sounds like you want to count characters instead. strlen() has a sibling named strchars() that does just that. Using your example string...

:echo strchars("°") 

...displays "1". That solves the "off by one" result you described, I think.

Related to DBK's observations in his comment, the function also takes an optional second parameter (Vim version >= 7.4.755) that dictates whether composing characters are counted or not. Quoting the function's docs, when {skipcc} is...

  • ...omitted or zero, composing characters are counted separately.
  • ...set to 1, Composing characters are ignored.

Not sure whether that part is relevant to you or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.