Solution to "enable Chinese garbled characters with vim and enable normal display with cat" in Ubuntu

Source: Internet
Author: User

System Environment: ubuntu10.04 complete installation of VIM gvim

Problem: The Chinese Characters in VIM on the terminal are garbled and some solutions are searched online. However, it is strange that none of these methods can implement gvim menus and Chinese characters in the text. The Chinese characters in the text under Vim can be displayed normally at the same time. After a scheme is applied, the Chinese characters (Chinese) in the files opened by VIM and gvim can be normally displayed, but the Chinese characters in gvim menus are garbled. After making various attempts to these solutions, find the following methods to achieve normal display of Chinese characters at the same time.

Open Terminal

[Plain]View plaincopy
  1. $ Sudo-S
  2. # Vim/etc/Vim/vimrc

Add the following code at the end of the file:

[Plain]View plaincopy
  1. Set fileencodings = gb2312, UTF-8
  2. Set fileencoding = gb2312
  3. Set termencoding = UTF-8

Save and exit. The problem is solved, but the specific cause of garbled characters is not clear yet.

Some references are provided below:

Http://doc.linuxpk.com/80308.html

1. Support for Chinese Encoding

To better support Chinese encoding, VIM requires two features: + multi_byte and + iconv. You can use the |: Version | command to check whether the currently used Vim is supported. Otherwise, you need to re-compile the code.

2. settings that affect Chinese Encoding

Several options in VIM affect multi-byte encoding support:

  • Encoding (ENC): encoding is the internal encoding of VIM. The encoding setting affects the buffer and message text in Vim. In UNIX, encoding is set to locale by default. In Windows, it is the same as the current code page. In Chinese Windows, the default encoding setting is cp936 (GBK ).
  • Fileencodings (fenc): Vim identifies the file Encoding Based on the fileencodings option when opening a file. fileencodings can set multiple encodings at the same time, vim guesses the encoding of the opened file based on the set sequence.
  • Fileencoding (fencs): Vim saves the new file according to the fileencoding encoding when saving it. If an existing file is opened, VIM saves the file according to the encoding recognized when the file is opened, unless fileencoding is reset during saving.
  • Termencodings (tenc): When Vim is used in the terminal environment, the termencoding item is used to indicate the encoding used by the vim terminal.

3. encoding conversion in VIM

Vim internally uses the iconv library for encoding conversion. If the encoding settings of these options are inconsistent, VIM may convert the encoding. When an existing file is opened, it is converted from the file encoding to the encoding set by encoding. When saving the file, it is converted from the encoding set by encoding to the encoding corresponding to fileencoding. The Vim prompt [converted] is often displayed, which indicates that the vim code is converted internally. If Vim is used in the terminal environment, the encoding set by termencoding is converted to the encoding set by encoding.

You can use |: Help encoding-values | to list all the encodings supported by VIM.

4. Specific Application Environment Settings

  • Edit only GBK-encoded files

Set fileencodings = cp936
Set fileencoding = cp936
Set encoding = cp936

  • Only edit Chinese files of UTF-8 Encoding

Set fileencodings = UTF-8
Set fileencoding = UTF-8
Set encoding = cp936, or set encoding = UTF-8

  • Supports both GBK and UTF-8 Encoding

Set fileencodings = ucs-bom, UTF-8, cp936
Set fileencoding = UTF-8
Set encoding = cp936, or set encoding = UTF-8

  • If Vim is used in the terminal environment, set termencoding to be consistent with the encoding used by the terminal. For example:

Set termencoding = cp936 or set termencoding = UTF-8

When you edit an UTF-8 encoding file in Windows notepad, the three-byte BOM: efbbbf is added to the file header. In fileencodings, if you set the NFS-bom to be compatible with files edited in notepad, you can remove it if you do not need it. Vim removes the BOM when saving the UTF-8-encoded file. The best thing to remove Bom is that you can use Cat a B> C in UNIX to correctly Merge files, which is often ignored.

5. FAQs

  1. Why can only half of Chinese characters be deleted at one time in Vim?

    The encoding setting is incorrect. Setting encoding to cp936 can solve this problem. In UNIX, VIM sets the default encoding according to locale. If locale is not correctly set and encoding is not set, only half Chinese characters can be deleted at a time.

  2. Why can't Vim enter traditional Chinese characters?

    Change EUC-Cn or gb2312 to cp936. EUC-CN is an alias for gb2312 and does not support traditional Chinese characters. Cp936 is an alias of GBK and a superset of gb2312. It supports traditional Chinese characters.

  3. Why can't Vim be converted?

    Because the iconv option is not added during vim compilation, re-compile Vim to solve the problem.

  4. How do I open a GBK encoded file and save it as UTf-8 encoding?

    Run the command when saving the file |: Set fileencoding = UTF-8 |.

Solution to "enable Chinese garbled characters with vim and enable normal display with cat" in Ubuntu

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.