Solution to linux under vim Chinese garble

  • 2020-05-14 05:55:16
  • OfStack

A detailed introduction to the Vim encoding

Like all popular text editors 1, Vim is good for editing various character encodings, including UCS-2, UTF-8, and other popular Unicode encodings.

Vim has four options related to character encoding, encoding, fileencoding, fileencodings, termencoding (for possible values of these options, please refer to Vim online help :help encoding-names), which have the following meanings:

1. encoding: the character encoding method used internally by Vim, including buffer (buffer) of Vim, menu text, message text, etc. The user's manual recommends only changing its value in.vimrc, and in fact it seems to make sense only to change its value in.vimrc.

2, fileencoding: Vim file of the current editor in character encoding, Vim save the file when he saves the file for this kind of character encoding (regardless of whether the new file), the Internet is such, but I do. Defined vimrc utf - 8 seems to have no effect, can only be opened in vim files manually will work, don't know why.

3. fileencodings: when Vim is started, it will probe the character encoding mode of the file to be opened one by one according to the character encoding mode listed in Vim, and set fileencoding as the final detected character encoding mode. So it's best to put Unicode at the top of the list and latin1 at the bottom.

4. termencoding: the character encoding method of the terminal where Vim works (or Console window of Windows). This option is not valid under Windows for GUI mode gVim, which is the Windows console code page for Console mode Vim, and we usually don't need to change it.

Let's take a look at how Vim's multi-character encoding support works.

1. Start Vim and set the character encoding mode of buffer, menu text and message text according to the value of encoding set in.vimrc.

2. Read the file that needs to be edited, and probe the encoding mode of the file one by one according to the character encoding mode listed in fileencodings. And set fileencoding to detect what appears to be the correct character encoding, and if no appropriate encoding is found, turn it on with the latin-1 (ASCII) encoding.

3. Compare the values of fileencoding and encoding. If they are different, call iconv to convert the contents of the file to the character encoding method described by encoding, and put the converted contents into buffer for this file.

4. When saving the file after editing, compare the values of fileencoding and encoding again. If not, call iconv again to convert the text in buffer to the character encoding described by fileencoding and save it to the specified file.

Since Unicode can contain characters from almost any language, and Unicode's UTF-8 encoding is a very cost-effective encoding (less space consumption than UCS-2), it is recommended that encoding be set to utf-8. Another reason for doing this is that when encoding is set to utf-8, Vim automatically detects files that are encoded more accurately (perhaps this is the main reason;). . For the files we edited in Chinese Windows, GB2312/GBK is appropriate for compatibility with other software. Therefore, fileencoding is recommended to be set to chinese (chinese is an alias, which means gb2312 in Unix and cp936 in Windows, which means the code page of GBK).

The solution to vim Chinese garble under linux

1, download

To http: / / www. vim. org vim source code/download the latest version 7.3.

2, installation,

Before you compile, first ./configure --help See 1 for configuration options,

This configuration needs to be included :--enable-multibyte      Include multibyte editing support

Its role is to support multi-byte encoding, and this step should be important. I don't know if I'm right.

When configured, nature is normal: make , make install the

3. Finally, the vimrc script is configured

The editor ~/.vimrc Add the following lines to the file:


 set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
 set termencoding=utf-8
 set encoding=utf-8

You're done.

conclusion


Related articles: