How to solve vim Chinese garbled characters in linux

Source: Internet
Author: User

When editing a Chinese text in vim, it sometimes looks garbled. I used to fix and complete the text without Garbled text. This problem has always plagued me. I suddenly thought of this problem, so I wanted to solve it. I checked it online for a long time. I had a lot of information about this, but it was not very well done. After a day of hard work, I uninstalled vim many times, installed it many times, and did some small tests. Today I finally got it done.

 

1. Download

Go to http://www.vim.org/to download the latest version 7.3's vimsource code.

 

2. Installation

Before compilation, check the configuration options in./configure -- help,

Here, this configuration needs to be added: -- enable-multibyte Include multibyte editing support

Its function is to support multi-byte encoding. This step should be very important. Otherwise, the subsequent configuration seems useless. I don't know if I'm right or not.

 

After the configuration is complete, it is common: make, make install

 

3. Finally, the vimrc script is configured

Edit ~ /. Add the following lines to the vimrc file:

Set fileencodings = UTF-8, ucs-bom, gb18030, gbk, gb2312, cp936
Set termencoding = UTF-8
Set encoding = UTF-8

You can do this.

 

By the way, record the Vim encoding details

Vim, like all popular text editors, can edit a variety of character-encoded files, which of course include popular Unicode encoding methods such as UCS-2 and UTF-8.
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:
(1) encoding: The character encoding method used inside Vim, including the buffer (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.
(2) fileencoding: The character encoding method of the file currently edited in Vim. When Vim saves the file, it also saves the file as this encoding method (whether new files are used or not ), this is an introduction on the Internet, but I am doing this. the definition of UTF-8 in vimrc does not seem to work. It can only be set manually when the vim file is opened. I don't know why.
(3) fileencodings: When Vim is started, it will detect the character encoding methods of the files to be opened one by one according to 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.
(4) 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.

Record how Vim supports multi-character encoding.
(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. Set fileencoding to the correct character encoding method. If no proper encoding is found, use Latin-1 (ASCII) encoding.
(3) Compare 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. Now we can edit this file.
(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.

Since Unicode can contain characters in almost all languages, and the Unicode UTF-8 encoding method is very cost-effective (space consumption is less 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 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, gb2312 in UNIX, cp936 in windows, that is, the GBK code page ).

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.