Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expression to transform unicode codepoint to character

Tags:

vim

unicode

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?

like image 472
rampion Avatar asked Dec 01 '25 07:12

rampion


1 Answers

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
like image 158
Kent Avatar answered Dec 03 '25 22:12

Kent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!