Convert escaped codepoint to unicode character

2019-04-07 05:48发布

问题:

I am trying to take a chunk of JSON that has strings which contain the literal characters \u009e and I would like to convert those characters to its associated single unicode character, in this case é.

I use curl or wget to download the json which looks like:

{ "name": "Kitsun\u00e9" }

And need to translate this in Vim to:

{ "name": "Kitsuné" }

My first thought was to use Vim's iconv, but it does not evaluate the string as a single character and just returns the input.

let code = '\u00e9'
echo iconv(code, "UTF-8", "UTF-8")
" Prints \u00e9

I want to eventually use something like

%s;\\u[0-9abcdef]*;\=iconv(submatch(0),"UTF-8", "UTF-8");g

回答1:

this line works for your example:

s#\\u[0-9a-f]*#\=eval('"'.submatch(0).'"')#

or

s#\v\\u([0-9a-f]{4})#\=nr2char(str2nr(submatch(1),16))#


标签: vim utf-8 iconv