Share the analysis of garbled characters in the vim editor of Windows System

Source: Internet
Author: User
From: http://www.cnblogs.com/yongtan/archive/2011/09/21/2183014.html

It seems that we have encountered Chinese garbled characters in VIM in windows before, I just used it a little and I am too lazy to try it out. // This problem was not found until I tried a Vim plug-in introduced by caspatant last night to implement the Twitter client function --- twitvim. the message is garbled at all... So I decided to solve the problem of Chinese garbled characters caused by the vim editor encoding method ///

After learning about the character encoding method, VIM has four character encoding options: encoding, fileencoding, fileencodings, and termencoding. For the possible values of these options, see Vim online help: help encoding-names), their respective meanings:
* Encoding: the internal character encoding method used by VIM, including the buffer, menu text, and Message Text of vim. In the user manual, it is recommended to change its value only in. vimrc. In fact, it seems only meaningful to change its value in. vimrc.
* 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: When Vim is started, it detects the character encoding methods of the files to be opened one by one based on the character encoding methods listed in it, and sets fileencoding as the final character encoding method. 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. 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.
Since Unicode can contain characters in almost all languages, the Unicode UTF-8 encoding method is very cost-effective, so the value of encoding is set to UTF-8. When encoding is set to UTF-8, VIM automatically detects more accurate file encoding methods. To ensure compatibility with other software, we recommend that you set the file encoding to gb2312/GBK. Therefore, fileencoding is set to Chinese (Chinese is an alias, indicates gb2312 in UNIX, cp936 in windows, that is, the GBK code page ).
Finally, for the solution that displays Garbled text, menu Garbled text, right-click menu Garbled text, and conlse output Garbled text in the file, modify the configuration file _ vimrc corresponding to the vim editor and add the following configuration:

"Garbled characters are displayed in the processed text.
Set encoding = UTF-8
Set fileencodings = UTF-8, Chinese, Latin-1
If has ("Win32 ")
Set fileencoding = Chinese
Else
Set fileencoding = UTF-8
Endif

"Process menu and context menu garbled characters
Source $ vimruntime/delmenu. Vim
Source $ vimruntime/menu. Vim

"Handling consystemic output garbled characters
Language messages zh_CN.utf-8

How Vim supports multi-character encoding:
Start vim and set the encoding of the buffer, menu text, and message text based on the encoding value set in the _ vimrc configuration file.
Next, read the file to be edited and test the file encoding method one by one based on the character encoding methods listed in fileencodings. Set fileencoding as the detected character encoding method.
Then, compare the fileencoding and encoding values. 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. After that, you can edit the file.
Finally, when saving the file after editing, compare the values of fileencoding and encoding again. If you call iconv again, convert the text in the buffer to the character encoding method described in fileencoding and save it to the specified file.
Note: You need to call the external iconv. dll and ensure that the file exists in $ vimruntime or other directories listed in PATH environment variables.

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.