Like all popular text editors, VIM can well edit a variety of character encoding files, which of course include popular unicode encoding methods such as UCS-2 and UTF-8. Unfortunately, like a lot of software from the Linux world, you need to set it yourself. Vim has four options related to the character encoding method: encoding, fileencoding, fileencodings, and termencoding. for possible values of these options, see Vim online help: Help encoding-names ), their meanings are as follows: * Encoding: the internal character encoding method used by VIM, including the buffer, menu text, and Message Text of vim. 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 saving the file, VIM also saves the file as this encoding method (whether new files are used 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 method 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. Well, I have explained this pile of parameters that will easily confuse new users. Let's take a look at how Vim's multi-character encoding method supports work. 1. Start vim and set the encoding mode of the buffer, menu text, and message text based on the encoding value set in. vimrc. 2. Read the file to be edited and test the file encoding method one by one based on the character encoding methods listed in fileencodings. And set fileencoding to the detected character encoding method, which looks correct (note 1. 3. compare the values of fileencoding and encoding. If they are different, call iconv to convert the file content to 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). You need to ensure that this file exists in $ vimruntime or other columns in the PATH environment variable directory. 4. When saving the file after editing, compare the values of fileencoding and encoding again. If different, call iconv again 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 because Unicode can contain characters in almost all languages, and the Unicode UTF-8 encoding method is a very cost-effective encoding method (less space consumption than UCS-2 ), therefore, we recommend that you set the encoding value to UTF-8. Another reason for doing so is that when encoding is set to UTF-8, VIM will automatically detect more accurate file encoding methods (maybe this is the main reason ;). For Files edited in Chinese Windows, to ensure compatibility with other software, the file encoding is set to gb2312/GBK. Therefore, fileencoding is recommended to be set to Chinese (Chinese is an alias, indicates gb2312 in UNIX, cp936 in windows, that is, the GBK code page ). Here is my. in vimrc (see the attachment), the character encoding method settings are flexible. You can use the environment variable $ Lang in the system (% Lang % in Windows) to automatically set the appropriate character encoding method. At this point, it is recommended to set % Lang % = zh_CN.UTF-8, you can easily through the Windows registry script file. 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 it may cause another minor problem if you want Vim to display chinese menus and prompt messages. NOTE 2: You can download to iconv Win32 on gnu ftp. (Http://mirrors.kernel.org/gnu/libiconv/libiconv-1.9.1.bin.woe32.zip), it is not recommended to go to gnuwin32 (http://gnuwin32.sourceforge.net/) download libiconv, because that version is older, and you need to 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 'ucs-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"
" Japanese, 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
" 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 MacRoman (Macintosh encoding)
1 8bit-{name} any 8-bit encoding (Vim specific name)
1 cp437 similar to iso-8859-1
1 cp737 similar 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 Japanese (Windows only)
2 euc-jp Japanese (Unix only)
2 sjis Japanese (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 Japanese: 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 the
environment |