开发者

vim unicode bufread/bufwrite script

Problem:

I want my unicode characters to be stored on disk as (rather tan utf8/16 encoding)

\u####

However, I want them dispalyed as unicode characters when opened up in vim.

I think the easiest way to acheive this is some bufopen/bufwrite script that automatically:

on opening, convert \u#### to unicode character

on writing, convert unicode characters into \开发者_JAVA技巧u####

However, I don't know what functions to call to make this happen. Can someone lend a hand?

Thanks!


I made this plugin just for you!

http://www.vim.org/scripts/script.php?script_id=909

Vim file plugin for editing files with unicode codes.

It changes all of the codes to the accented characters for viewing, and turns all accented characters into the code when writing.

e.g. it changes \u00E9 to é when viewing, and puts \u00E9 when writing (java-style encoding). Set g:ucs_encode_java (in your .vimrc file)

By default, it works for all the accented characters in the Unicode Latin-1 supplement, but you can quickly change it for your needs.

You can also set it to work with html encoding (&#nnn;). Set g:ucs_encode_html (in your .vimrc file)

You can also set it to convert accented characters to octal encoding ( \340 ). Set g:ucs_encode_octal in your .vimrc file

Roger


You will need an external tool to convert between the two formats. I recommend Bill Poser's uni2ascii:

$ echo täßt | uni2ascii -q -a L
t\u00E4\u00DFt
$ echo täßt | uni2ascii -q -a L | ascii2uni -q -a L
täßt

Then you have to tell Vim to use that filter. This is only the most rudimentary method (I'm no good at that part of vim scripting):

autocmd BufNewFile,Bufread *.u :%!ascii2uni -q -a L
autocmd BufWritePre        *.u :%!uni2ascii -q -a L
autocmd BufWritePost       *.u :%!ascii2uni -q -a L

Change the filename pattern to whatever fits your case.

There is a more advanced example for a related problem included in the docs: :help hex-editing. A discussion of this approach and an even better solution can be found in the Vim wiki.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜