Reprinted from:-Tiansheng's diary-NetEase Blog
There are two problems with the character encoding of Vim/gvim under Chinese Windows:
- No encoding detection function by default
- If a file itself uses a character set larger than GBK (such as UTF-8, UTF-16, GB18030), then the characters in the GBK cannot be garbled and will be lost when saved. Even if the file format is correctly detected when editing a file, it does not help.
The solution to the first problem is to add the following configuration to the ~/.VIMRC:
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
The solution to the second problem is to force the internal encoding of Vim to use some kind of UTF encoding. such as UTF-8:
set encoding=utf-8
However, setting the internal code of Vim to UTF-8 brings up the following new problems
- Use of Vim with non-GUI interface is garbled
- Hint information (e.g.
E492: Command not editor: Foo
) garbled
To solve the non-GUI interface of vim garbled problem, you need to set the terminal code for the system default encoding:
set termencoding=cp936
To make the message is not garbled, you need to use the UTF-8 version of the prompt message:
language messages zh_CN.UTF-8
To sum up, in the Chinese Windows correctly configured character encoding, you need to add the following content in your ~/.VIMRC
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
set encoding=utf-8
set termencoding=cp936
language messages zh_CN.UTF-8
In particular, the above code should be placed at the top of the. VIMRC, because Vim set encoding=xxx
is dangerous during operation and can result in various garbled characters (see here). My own use of the complete. VIMRC placed in HTTPS://SITES.GOOGLE.COM/SITE/POPATRY/ETC/-VIMRC, continuously updated.
Configuration. VIMRC solve the character encoding problem of Vim/gvim in Chinese Windows