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?
1. Use VIM to check the file encoding
You can directly view the file encoding in Vim
:set fileencoding
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
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!