rampion
rampion

Reputation: 89113

expression to transform unicode codepoint to character

I've got a file that has escaped unicode in it:

blah blah blah \u2192 blah blah blah

I'd like to transform the unicode escapes into actual characters with a search/replace:

:%s/\\u\(\d\{4}\)/\=CodePointToCharacter(submatch(1))/g

Though I know how to transform a codepoint to a character in insert mode (CTRL-V u 2192 for →), I don't know how to do the transformation in a vimL expression.

Do I need to write a custom function, or is there a builtin or plugin I can use?

Upvotes: 3

Views: 191

Answers (1)

Kent
Kent

Reputation: 195229

you can first use str2nr() to convert the number from HEX, then use nr2char to display the char.

extend your command a little bit:

%s/\\u\(\x\{4}\)/\=nr2char(str2nr(submatch(1),16))/g

Upvotes: 6

Related Questions