Vim character encoding

Source: Internet
Author: User
Vim encoding method settings

Reprinted: http://www.cnblogs.com/freewater/archive/2011/08/26/2154602.html

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, while Vim in Console mode is Code Page, 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 ).

For fedora, VIM settings are generally placed in the/etc/vimrc file. However, we recommend that you do not modify them. Can be modified ~ /. Vimrc file (which does not exist by default. You can create one by yourself) and write the desired settings.

My. vimrc file is as follows:

 

: Set Encoding = UTF-8 : Set Fileencodings = ucs-bom, UTF-8, cp936 : Set Fileencoding = gb2312 : Set Termencoding = UTF-8

Among them, fileencoding configuration can be set to UTF-8, but my MP3 does not seem to support UTF-8 encoding, so simply set it to gb2312. Now, it is fixed that the TXT file with no garbage codes can be displayed in both the VI and MP3.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.