Home > System Tutorial > LINUX > body text

How to convert file encoding formats under Linux

PHPz
Release: 2024-02-09 22:51:23
forward
856 people have browsed it

When operating files under Windows in the Linux operating system, you often encounter a very troublesome problem - garbled file encoding. Especially when you need to compile a program written in Windows on a Linux host, this problem will become particularly serious, because it will not only affect the display of Chinese comments, but also cause the compiler to report an error. So, in this article, we will summarize for you the methods to solve the problem of Windows file encoding in Linux, so that you can easily deal with this problem.

This is because the default file format in Windows is GBK (gb2312), while Linux is generally UTF-8. So how to check the encoding of a file and how to convert the encoding of the file in Linux?

How to convert file encoding formats under Linux

1. Use VIM to check the file encoding

You can directly view the file encoding in Vim

:set fileencoding
Copy after login

will display the file encoding format.

2. Rewrite the ~/.vimrc file

If you just want to view files in other encoding formats or want to solve the problem of using Vim to view garbled files, then you can

Add the following content to the ~/.vimrc file:

set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936
Copy after login

In this way, vim can automatically identify the file encoding (it can automatically identify UTF-8 or GBK encoded files). In fact, it is to try according to the encoding list provided by fileencodings. If no suitable encoding is found, use latin-1( ASCII) encoding is turned on.

3. Convert using ICONV file encoding

We use the iconv tool to convert the encoding of the file.

iconv conversion, the command format of iconv is as follows:

iconv -f encoding -t encoding inputfile

For example, convert a GBK encoded file into UTF-8 encoding

iconv -f GBK -t UTF-8 file1 -o file2

The meaning of the parameters

-f From a certain encoding

-t To a certain encoding

-o Output to file

4. Change the file encoding by saving as in Notepad;

Open it with Notepad under Windows, select Save As..., and change the encoding. ****

In short, whether you check the file encoding through VIM, rewrite the ~/.vimrc file, use ICONV file encoding conversion, or change the file encoding through Notepad Save As, it can help you solve Windows file encoding problems in Linux. Hope this article helps you!

The above is the detailed content of How to convert file encoding formats under Linux. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template