Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of File Encoding processing and reopening garbled Files in Vim

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Vim file coding processing and re-open garbled file example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

Preface

There are two coding-related variables in Vim, and if you understand them, you will no longer have a headache for coding problems.

Encoding: Vim internal coding, such as buffer, registers, text, and so on. This value is generally not set by users, and it doesn't make sense to set this value after opening Vim. You can think of this value as a variable of the Vim program itself. If you encounter a file coding problem at work, it has nothing to do with the variable encoding.

Fileencoding: as the name implies, it is the encoding of the file.

In addition, there is a value called fileencodings is a plural. Generally, we set this value in vimrc. When Vim opens a file, it guesses the encoding of the file according to the order set in fileencodings. For example, set it like this:

Set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

So sometimes Vim guesses wrong, what if the open file shows garbled code? (ps: usually when Vim opens a file, it's garbled because you don't have a code in your fileencodings, so Vim doesn't guess correctly. For example, if you delete gb18030 from the above settings, when you open this encoded file, you will find that the value of fileencoding is latin1, while the file is garbled.)

At this point you may want to set the value of fileencoding, but at this time our file has been opened, you will find that the status of Vim buffer has changed to Edited. However, the file still shows garbled code and there is no change. The specific reasons will be explained in detail later.

The correct thing to do is to reopen the file with a specific encoding, such as using the reopen command: e + + enc=gb2312 in Vim, where + + enc is an option to specify the encoding to be used. After opening it, you will find that Vim has opened the file in the form you specified, but the file has become readonly. If you want to change it, just set: set noreadonly.

In fact, the principle is a bit like the sandwich model proposed by someone in Python:

When Python takes the bytes from the stream (for example, the network, the file I Vim O), it becomes str through decode (), and when Vim reads a file, it converts it to internal encoding encoding based on fileencoding (set by the user or guessed by fileencodings).

When Python writes to a file, it changes to bytes with encode () and then writes. When Vim writes from buffer to file, it also converts the data from internal encoding to fileencoding and then writes it.

This explains why it is useless to modify fileencoding in Vim when the code is garbled.

Because setting the value of fileencoding after opening the file will not change the data that has been loaded into Vim buffer, at this time the data has been converted, this setting will only change the time of writing using the current fileencoding to write, so to sum up, "Open the file using one encoding, write the file using another encoding".

The requirement for correct garbled code should be: I want to open this file in a specific encoding form.

Thank you for reading this article carefully. I hope the article "sample Analysis of File Encoding processing and reopening garbled Files in Vim" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report