5/22/07

Vim, Gvim, utf8, and Chinese in Windows XP

See the post for using chinese in dos (cmd) under Windows XP (English).

If you have set locale to PRC as in above post, you are all set to input Chinese in Vim and Gvim. Do a ":set encoding" in vim/gvim, it should say the encoding is set to cp936 (codepage 936, ie. GBK/GB2312).
This is fine if you want to save file as encoded in GB2312. However I usually save all my file as UTF8. To do this I can issue ":set fileencoding=utf8" in vim, but I have to do it every time for a new file. Also, when you open a file previously saved as utf8 in Vim, it will displayed incorrectly because Vim assume it's encoded in gb2312. You can ask vim to reload a file with utf8 encoding by issue a ':e ++enc=utf8', but again you have to do it every time.

To solve these 2 problems, we need to edit our _vimrc file(located in Program Files\vim), and add these 2 lines:
set fileencoding=utf8
set fileencodings=ucs-bom,utf8,prc

The first line tell Vim to save any new file encoded in utf8, the 2nd line tell it to detect file encoding in that order.

That's it, now we can edit both utf8 and GB encoded file.

If you want to make Gvim look prettier when displaying Chinese, add following lines in _vimrc (assume you already have Chinese font installed):
if has("gui_running")
set guifont=NSimSun:h12:cGB2312
endif

7 comments:

Anonymous said...

Hi, I'm a heavy VIM user. But I don't know how to use VIM to enter/display Chinese. (BTW, I have TwinBridge and quite experienced in entering Chinese in Word and webpage.)

Can you help me to set up the Chinese on GVim? I have read the GVIM website. Seem some requirements about font loading are needed. But the instructure isn't very clear.

It'd very much appreciated if you could help. Many thanks in advance.

ML
email: funchip@yahoo.com

Anonymous said...

This is fully wicked man... cheers for that.
lei [dot] chen [at] icnpro [dot] com

Anonymous said...

For inputing Chinese, please check vimim —— vim 中文输入法
http://maxiangjiang.googlepages.com/vimim.html

Ben Anderson said...

I don't have the NSimSun font. Looks like you have to purchase it? I can't seem to find any unicode/fixed-width/chinese font. Any suggestions? I've found several free chinese fonts, but none that are unicode AND fixed width. At least none that gvim is detecting.

Anonymous said...

The font NSimSun (SimSun) is being shipped with Windows, you have to activate the "Asian languages support" in "Control Pannel" > "Regions and languages".

On Linux, utf8 and Chinese work very well together, but the GTK-GUI version of Vim is very sluggish, so I use WinXP now.

On Windows I'm trying to set Courier New for Western languages and SimSun for Chinese - no success yet.
I think, ":set guifontset" is the right direction. Consult ":help guifontset"

Anonymous said...

Solved:

(_vimrc file:)

[...]
set encoding=utf-8
set guifont=Courier_New:h12
set guifontwide=NSimsun:h12
[...]

Anonymous said...

Cheers for posting that, I have made a note of it here. I would have assumed Win32 would default to UTF-8 but alas not so.

I discovered this when I transferred a file from my netbook (a Yeeloong running Gentoo Linux) to a laptop running Windows XP; I used the superscript 1 in a few places -- that was corrupted due to Latin1 encoding being used on Windows XP.

Whilst looking at _vimrc I noticed a cause for a second nit; and fixed that too... see :help behave if you're wondering why visual mode doesn't work the same on Win32 and Unix.