From: http://blog.chinaunix.net/u1/37553/showart_347927.html
Like all popular text editors, VIM can edit various character encoding files, including
Popular unicode encoding methods such as UCS-2 and UTF-8. Unfortunately, however
You need to configure it yourself.
Vim has four options related to character encoding methods: encoding, fileencoding, fileencodings,
Termencoding (for the possible values of these options, see Vim online help: Help encoding-names ).
The meaning is as follows:
* Encoding: the internal character encoding method used by VIM, including the vim buffer and menu
Text. By default, it is recommended to change the value of locale only in. vimrc in the user manual. In fact, it only makes sense to change the value in. vimrc. You can use another encoding method to edit and save files. For example, if your vim encoding is UTF-8, the edited file uses cp936 encoding, vim will automatically convert the Read File to UTF-8 (Vim can read), and when you write the file, it will automatically convert it back to cp936 (the file storage encoding ).
* Fileencoding: The character encoding method of the file currently edited in Vim. When Vim saves the file, it will also
Save as this character encoding method (whether new or not ).
* Fileencodings: Vim automatically detects the fileencoding sequence list. At startup, it detects the character encoding methods of the files to be opened one by one based on the character encoding methods listed in it, set fileencoding to the character encoding method that is finally detected. Therefore, it is best to put the Unicode encoding method at the beginning of this list, and put Latin1 in the latin1.
* Termencoding: The character encoding side of the terminal (or Windows Console window) operated by VIM
. If Vim is encoded in the same term as vim, you do not need to set it. Otherwise, you can use the termencoding option of VIM to automatically convert it to the term encoding. this option is invalid for gvim in common GUI mode in windows, but Vim in Console mode is the code page in Windows console, and we usually do not need to change it.
Okay, I have explained this bunch of parameters that will easily confuse new users. Let's take a look at the multi-character encoding supported by VIM.
How it works.
1. Start vim and set the buffer, menu text, and message according to the encoding value set in. vimrc.
Character encoding method.
2. Read the file to be edited and test the file one by one based on the character encoding methods listed in fileencodings.
Encoding method. And set fileencoding to the detected character encoding method, which looks correct (note 1.
3. Compare fileencoding and encoding values. If they are different, call iconv to convert the file content
The character encoding method described by encoding, and put the converted content in the buffer opened for this file,
Now we can edit this file. Note: To complete this step, you need to call the external iconv. dll
(Note 2) ensure that the file exists in $ vimruntime or other directories listed in the PATH environment variable.
.
4. When saving the file after editing, compare the values of fileencoding and encoding again. If different
Call iconv to convert the text in the buffer to the character encoding method described by fileencoding.
And save it to the specified file. Similarly, you need to call iconv. dll
Since Unicode can contain characters in almost all languages, and the Unicode UTF-8 encoding method is
Very cost-effective encoding method (less space consumption than UCS-2), so it is recommended that the value of encoding be set
UTF-8. Another reason for doing so is that when encoding is set to UTF-8, VIM automatically detects the file encoding method.
It will be more accurate (maybe this is the main reason ;). The files we edit in Chinese windows
For compatibility of other software, it is recommended that the file encoding be set to gb2312/GBK.
Set to Chinese (Chinese is an alias, expressed in Unix as gb2312, expressed in Windows
Cp936, that is, the GBK code page ).
The following is the content about character encoding method settings in my. vimrc (see the attachment). This setting is flexible and can be
Automatically set the appropriate value based on the value of the system environment variable $ Lang (of course, % Lang % in Windows)
. At this point, it is recommended to set % Lang % = zh_CN.UTF-8, which can be registered through the following Windows
Table script file to facilitate.
Note 1: in fact, the test accuracy of VIM is not high, especially when encoding is not set to UTF-8. Therefore
We strongly recommend that you set encoding to UTF-8, although if you want Vim to display the Chinese menu and prompt message
This will bring about another small problem.
NOTE 2: You can download to iconv Win32 on gnu ftp.
(Http://mirrors.kernel.org/gnu/libiconv/libiconv-1.9.1.bin.woe32.zip), not recommended to go
Gnuwin32 (http://gnuwin32.sourceforge.net/) download libiconv because that version is older and
You must rename the DLL file.
NOTE 3: View help: H iconv-dynamic
On MS-Windows Vim can be compiled with the | + iconv/Dyn | feature. This means
Vim will search for the "iconv. dll" and "libiconv. dll" libraries. When
Neither of them can be found Vim will still work but some conversions won't be
Possible.
--
Appendix 1: vimrc File
"Multi-encoding setting, must be in the beginning of. vimrc!
"
If has ("multi_byte ")
"When 'fileencodings 'starts with 'ucos-bom ', don't do this manually
"Set bomb
Set fileencodings = UCS-bom, Chinese, Taiwan, Japan, Korea, UTF-8, Latin1
"CJK environment detection and corresponding setting
If V: lang = ~ "^ Zh_cn"
"Simplified Chinese, on unix euc-CN, on MS-Windows cp936
Set encoding = Chinese
Set termencoding = Chinese
If & fileencoding =''
Set fileencoding = Chinese
Endif
Elseif V: lang = ~ "^ Zh_tw"
"Traditional Chinese, on unix euc-TW, on MS-Windows cp950
Set encoding = Taiwan
Set termencoding = Taiwan
If & fileencoding =''
Set fileencoding = Taiwan
Endif
Elseif V: lang = ~ "^ Ja_jp"
"Japan, on unix euc-JP, on MS-Windows cp932
Set encoding = Japan
Set termencoding = Japan
If & fileencoding =''
Set fileencoding = Japan
Endif
Elseif V: lang = ~ "^ Ko"
"Korean on unix euc-KR, on MS-Windows cp949
Set encoding = Korea
Set termencoding = Korea
If & fileencoding =''
Set fileencoding = Korea
Endif
Endif
& Quot; detect UTF-8 locale, and override CJK setting if needed
If V: lang = ~ "Utf8 $" | V: lang = ~ UTF-8 $"
Set encoding = UTF-8
Endif
Else
Echoerr 'Sorry, this version of (g) Vim was not compiled with "multi_byte "'
Endif
Appendix 2:
Supported 'encoding' values are: * encoding-values *
1 Latin1 8-bit characters (ISO 8859-1)
1 iso-8859-n iso_8859 variant (n = 2 to 15)
1 koi8-r Russian
1 koi8-u Ukrainian
1 macroman (Macintosh encoding)
1 8bit-{name} Any 8-bit encoding (Vim specific name)
1 cp437 similar to iso-8859-1
1 cp737similar to iso-8859-7
1 cp775 Baltic
1 cp850 similar to iso-8859-4
1 cp852 similar to iso-8859-1
1 cp855 similar to iso-8859-2
1 cp857 similar to iso-8859-5
1 cp860 similar to iso-8859-9
1 cp861 similar to iso-8859-1
1 cp862 similar to iso-8859-1
1 cp863 similar to iso-8859-8
1 cp865 similar to iso-8859-1
1 cp866 similar to iso-8859-5
1 cp869 similar to iso-8859-7
1 cp874 Thai
1 cp1250 Czech, Polish, etc.
1 cp1251 Cyrillic
1 cp1253 Greek
1 cp1254 Turkish
1 cp1255 Hebrew
1 cp1256 Arabic
1 cp1257 Baltic
1 cp1258 Vietnamese
1 CP {number} ms-Windows: Any installed single-byte codePage
2 cp932 Japan (Windows only)
2 EUC-JP Japanese (unix only)
2 sjis Japan (unix only)
2 cp949 Korean (UNIX and Windows)
2 EUC-Kr Korean (unix only)
2 cp936 Simplified Chinese (Windows only)
2 EUC-CN Simplified Chinese (unix only)
2 cp950 traditional Chinese (on UNIX alias for big5)
2 big5 traditional Chinese (on Windows alias for cp950)
2 EUC-tw traditional Chinese (unix only)
2 2byte-{name} Unix: Any double-byte encoding (Vim specific name)
2 CP {number} ms-Windows: Any installed double-byte codePage
U UTF-8 32 bit UTF-8 encoded Unicode (ISO/IEC 10646-1)
U ucs-2 16 bit UCS-2 encoded Unicode (ISO/IEC 10646-1)
U ucs-2le like ucs-2, little endian
U UTF-16 ucs-2 extended with double-words for more characters
U utf-16le like UTF-16, little endian
U ucs-4 32 bit UCS-4 encoded Unicode (ISO/IEC 10646-1)
U ucs-4le like ucs-4, little endian
The {name} can be any encoding name that your system supports. It is passed
To iconv () to convert between the encoding of the file and the current locale.
For MS-Windows "CP {number}" means using codePage {number }.
Several aliases can be used, they are translated to one of the names above.
An incomplete list:
1 ANSI same as Latin1 (obsolete, for backward compatibility)
2 Japan: On Unix "EUC-JP", on MS-Windows cp932
2 Korea Korean: On Unix "EUC-KR", on MS-Windows cp949
2 PRC Simplified Chinese: On Unix "EUC-CN", on MS-Windows cp936
2 Chinese same as "PRC"
2 Taiwan traditional Chinese: On Unix "EUC-tw", on MS-Windows cp950
U utf8 same as UTF-8
U Unicode same as ucs-2
U ucs2be same as ucs-2 (big endian)
U ucs-2be same as ucs-2 (big endian)
U ucs-4be same as ucs-4 (big endian)
Default stands for the default value of 'encoding', depends on
Environment