Completely solves the problem of vim garbled characters

Source: Internet
Author: User
When you use vim to open a file containing Chinese characters, garbled characters may occur due to improper configuration. in this case, do not panic. put the following settings in your vimrc: setfencsutf-8, chinese, latin1fencutf-8encutf-8 multiple configurations, can be written in a line, just... when you use vim to open a file containing Chinese characters, garbled characters may occur due to improper configuration. in this case, do not panic. put the following settings in your vimrc: set fencs = UTF-8, chinese, latin1 fenc = UTF-8 enc = UTF-8
Multiple configurations can be written in one row. you only need to write one set. the configuration is analyzed in detail below. www.2cto.com prepare the test file.
I use the Ubuntu 12.04 system, which uses UTF-8 encoding by default. you can use iconv to transcode a UTF-8 encoded file to GBK. # use iconv to transcode UTF8 to GBK and save it to the file File.txt $ echo. Hello, world! '| Iconv-f UTF8-t GBK> file.txt there are three encoding methods for simplified Chinese: GB2312, GBK, GB18030. among them, GB18030 is the latest version, and GBK is the most widely used.
Windows XP uses GBK as the simplified Chinese encoding method. it is worth mentioning that when the software reads text files to the memory, it needs to convert the encoding method, which is generally stored in the memory using UTF8 or other unicode encoding. therefore, when reading a text file, the editor must know the file encoding method (ASCII, UTF8, GBK or Latin1 ). the advanced editor generally tries different encodings until it finds a suitable one (based on no error reported ). use Vimto open file.txt # to avoid vimrc/plugin impact, use '-u none' to not load vimrc/plugin. vim uses the default configuration $ vim-u NONE file.txt ää ° plugin, Ê À ½ ç! Vim does not report an error. Therefore, vim believes that the decoding is successful. because of garbled characters, we subjectively think that vim decoding fails. the philosophical question "Yes/non" is involved here, which is not discussed in depth. because I have not configured vim, vim can decide to decode the file in any way. it is not hard to find that vim is decoded using the omnipotent latin1. view encoding-related settings www.2cto.com: set fencs? Fileencodings = ucs-bom, UTF-8, default, latin1: set fenc? Fileencoding = latin1: set enc? Encoding = when the utf-8fencs opens the file, vim uses the encoding provided in fencs one by one to decode the text until the appropriate one is found. after fenc succeeds in the previous step, the appropriate encoding name will be assigned to fenc; otherwise, no value is assigned. enc sets the internal encoding method used by vim. utf8is recommended.
The cause of the problem is not identified by GBK in fencs. Therefore, vim is not good at making claims and uses GBK to decode the file. we only need to set GBK to fencs and re-open the file: set fencs = UTF-8, chinese, latin1: e! Among them, the front and back positions of fencs encoding names are very important. if you put latin1 at the top, the subsequent encoding will never work. because latin1 uses a single byte (8 bit) to encode characters, any byte is a valid latin1 character. chinese is the alias of euc-cn. the words in the file are common characters, so any Chinese encoding method can be used to smoothly decode the files. www.2cto.com can also be used in a simpler way to temporarily change the decoding method: e ++ enc = chinese. This command is particularly useful when you guess the file encoding! Author K ++
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.