Vim encoding method settings
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 vim Buffer
(Buffer), menu text, message text, etc. The default setting is based on your locale. We recommend that you only use. vimrc in the user manual.
In fact, it seems that only in. vimrc
To make sense. 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
The read file is automatically converted to UTF-8 (Vim can read), and when you write the file, it is automatically converted back to cp936 (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 according to the character encoding methods listed in it, and
Fileencoding is set to the final detected character encoding method. Therefore, it is best to put the Unicode encoding method at the beginning of this list, and the Latin encoding method
Put Latin1 at the end.
* Termencoding:
The terminal used by VIM (or Windows
Console window)
. 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 to the term
This option is invalid for gvim in common GUI mode in windows, while Vim in Console mode is
The code page of the 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 converts the file content to the character encoding method described by encoding, and places the converted content to 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 directory of the PATH environment variable.
4. When saving the file after editing, compare fileencoding and encoding again
. If different, call iconv again to convert the text in the buffer to fileencoding.
The description of the character encoding method, and save it to the specified file. Similarly, this requires calling iconv. dll because Unicode can contain characters in almost all languages
Unicode UTF-8 encoding is a very cost-effective encoding method (less space consumption than UCS-2), so we recommend that you Encoding
The value is set to UTF-8. Another reason for this is that when encoding is set to UTF-8, VIM automatically detects more accurate file encoding methods.
(Maybe this is the main reason ;). For compatibility with other software, the file encoding is set to gb2312/GBK.
Therefore, we recommend that you set fileencoding to Chinese (Chinese is an alias, which indicates gb2312 in UNIX.
Cp936 in windows, that is, the GBK code page ).