I am trying to get the value of iskeyword
as character. When I run set iskeyword?
, I get iskeyword=@,48-57,_,192-255
. Using 48-57
as an example, how can I turn them back to characters? Is there any helper function in vim or lua that can help me do that?
2 Answers
You can use the nr2char()
function:
echo nr2char(48)
This will display the character for the number zero, '0'.
To print that first range using Vimscript you could, for example, do something like:
for i in range(48, 57)
echo nr2char(i)
endfor
That will print out characters representing numbers 0-9, one per line.
Alternatively, to put the associated characters in a list:
let chlist = map(range(48, 57), {_, val -> nr2char(val)})
Another function that does the same number-to-char conversions but can handle multiple numbers at once is list2str()
echo list2str(range(48, 57))
...or the equivalent "method call"...
echo range(48, 57)->list2str()
Both will display the string 0123456789
.
You can retrieve a string containing all (ASCII + 127-255) iskeyword characters via the following:
echo substitute(join(map(range(256), 'nr2char(v:val)'), ''), '[^[:keyword:]]', '', 'g')
In patterns \k
refers to characters in iskeyword
(it is dynamic with this option), however this cannot be easily negated. Instead we can use the class expression [:keyword:]
which can easily be negated in a character class via [^[:keyword:]]
. We create a length 255 string containing all of the characters of interest, and substitute out all non-keyword chars.