Created
December 17, 2011 19:30
-
-
Save pasela/1491146 to your computer and use it in GitHub Desktop.
Escape/Unescape unicode string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" unicodeescape.vim - Escape/Unescape unicode string. | |
function! UnicodeEscapeString(str) | |
let oldenc = &encoding | |
set encoding=utf-8 | |
let escaped = substitute(a:str, '[^[:alnum:][:blank:][:cntrl:][:graph:]]', '\=printf("\\u%04x", char2nr(submatch(0)))', 'g') | |
let &encoding = oldenc | |
return escaped | |
endfunction | |
function! UnicodeEscape() range | |
let oldreg = @x | |
execute 'normal gv"xy' | |
let @x = UnicodeEscapeString(@x) | |
execute 'normal gv"xp' | |
let @x = oldreg | |
endfunction | |
function! UnicodeUnescapeString(str) | |
let oldenc = &encoding | |
set encoding=utf-8 | |
let escaped = substitute(a:str, '\\u\([0-9a-fA-F]\{4\}\)', '\=nr2char("0x" . submatch(1))', 'g') | |
let &encoding = oldenc | |
return escaped | |
endfunction | |
function! UnicodeUnescape() range | |
let oldreg = @x | |
execute 'normal gv"xy' | |
let @x = UnicodeUnescapeString(@x) | |
execute 'normal gv"xp' | |
let @x = oldreg | |
endfunction | |
command! -range UnicodeEscape :<line1>,<line2>call UnicodeEscape() | |
command! -range UnicodeUnescape :<line1>,<line2>call UnicodeUnescape() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment