Basically title, the length of unicode characters as calculated by the len()
function is off by one, for example
:echo len("à")
2
or
:echo len("°")
2
is there a way to fix this, maybe by setting some options?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityBasically title, the length of unicode characters as calculated by the len()
function is off by one, for example
:echo len("à")
2
or
:echo len("°")
2
is there a way to fix this, maybe by setting some options?
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 withstrlen()
.
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...
Not sure whether that part is relevant to you or not.