Learning and thinking about Vim coding mode configuration

Source: Internet
Author: User
Tags configuration settings

Sorry, today, in March September 30, we are about to take a long holiday on the 11th day. We have a little bit of impetuousness in our hearts. We have basically finished our work. How can we be so impetuous to become a technical master, I decided to write an article on Weibo.

Today, a configuration file must contain Chinese characters, and my colleagues use it in windows. I use it in Ubuntu. Of course it is a great vim, And I will definitely encounter problems with the Chinese encoding format, you need to configure vimrc.

Here, we also take a look at the vim encoding configuration settings, which are recorded here for future forgetting.

The character encoding in VIM is mainly related to three parameters: ENC (encoding), fenc (fileencoding), and fencs (fileencodings)

Fenc is the encoding of the current file (open an existing file and create a new file). You can change fenc and then save the file as a different code. For example, I: Set fenc = UTF-8 and then: W will save the file as UTF-8, And: Set fenc = GBK and then: W will save the file as GBK.

This value does not matter whether the file can be correctly decoded when it is opened.

Fencs is a list of guesses that are decoded when an existing file is opened. This value is written to vimrc, and there is no correct method for determining the file encoding. Therefore, VIM can only guess the file encoding. For example, the setting in my vimrc is
Set fileencodings = UTF-8, GBK

Therefore, each time I open a file in Vim, I try to decode the fencs list in the decoding format. If there is no error in the intermediate decoding from start to end, VIM considers the file as encoded, I will not try again later.

At this time, the fenc value will be set to the final encoding value adopted by VIM. In command line mode, run set fenc under Vim to view the encoding format of existing files.
Of course, this may also cause errors. For example, if your file is GBK encoded, but in fact only one or two characters are Chinese, then they may also be decoded by UTF-8, this file will be mistakenly identified as UTF-8, resulting in incorrect decoding.

ENC is used to display the encoding format of vim. No matter what the final file is encoded, VIM will convert it to the current system encoding for processing, so that it can be correctly displayed in the current system. This is what ENC does.

The three fencs fenc ENC parameters can be viewed in the command line mode set fencs/fenc/ENC of vim.

From the process of opening an existing file, we can see the differences between the three parameters. First, VIM matches the decoded file or uses the fencs list. After correct decoding, set fenc to the current decoding format, after decoding is complete, a transcoding will be performed to convert the file into the encoding format referred to by ENC for display.

This is the role of the three configuration parameters!

Use Vim to open a GBK file. The default encoding format is utf8. converted is displayed at the bottom of VIM, indicating that the encoding format has been converted.

Create a new file. Of course, the default editing mode is to use the encoding format specified by ENC, which is generally utf8. When saving the file, we can use fenc to specify the encoding format of the current file, for example, set fenc = GBK. In Windows, the correct display can be achieved.

The display encoding format is ENC, and the current file encoding format is fenc. Vim supports fenc, which involves converting the file format to the display format.


Of course, if you want to study it in depth, it is best to look at the vim source code. Although I have the spirit of pursuing it, I am not nervous .. The Research on tools is just enough for teenagers.

The encoding parameter configuration is like this. So far, as the underlying software engineer, it is not very good to go deep into the underlying data.


Think deeply about the meaning of the encoding format utf8 GBK for text files.

A file is a piece of data lying in the memory, and the data is 01010111 binary. Binary data itself is meaningless, but encoding gives this data meaning.

Different from binary files, a text file is an encoding level. There is an encoding/decoding layer in the middle. This layer should be the work required by various editors, such as Vim Emacs.

For application programming, the underlying read/write function reads the most primitive binary data. The editor interprets and displays the original data according to the encoding format.

If you want to go deeper, there is still a way to read the editor source code without fear of suffering!

Different encoding formats certainly have different interpretations of the same binary data, which may cause garbled characters.

Learning and thinking about Vim coding mode configuration

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.