If you need to operate files in Windows in Linux, you may frequently encounter file encoding conversion problems. In Windows, the default file format is GBK (gb2312), while Linux is generally a UTF-8. The following describes how to view the file encoding in Linux and how to convert the file encoding.
View File Encoding
You can view the file encoding in Linux in the following ways:
1. You can directly view the file encoding in vim.
ShellCode
.: Set fileencoding
The file encoding format is displayed.
If you only want to view files in other encoding formats or want to solve the problem of using Vim to View File garbled characters, you can
~ /Add the following content to the vimrc file:
Shell code
1). set encoding = UTF-8 fileencodings = ucs-bom, UTF-8, cp936
In this way, VIM can automatically identify the file encoding (can automatically identify the UTF-8 or GBK encoding files), in fact, according to the fileencodings provided by the encoding list to try, if not find the appropriate encoding, it is opened in Latin-1 (ASCII) encoding.
2). enca (if this command is not installed in your system, you can use sudo Yum install-y enca to install it) to view the file encoding.
$ Enca filename
Filename: Universal Transformation Format 8 bits; UTF-8
CRLF line Terminators
Note that enca does not recognize some GBK-encoded files very well:
Unrecognized Encoding
2. Use the file command
$ file filename
file encoding conversion
1 ). convert the file encoding directly in Vim, such as converting a file into UTF-8 format
shell code
.: Set fileencoding = UTF-8
2 ). enconv conversion file encoding, for example, to convert a GBK encoded file into UTF-8 encoding, the operation is as follows
enconv-l zh_cn-x UTF-8 filename
3 ). iconv conversion. The iconv command format is as follows:
iconv-F encoding-T encoding inputfile
for example, converting a UTF-8-encoded file into GBK encoding
iconv-f gbk-T UTF-8 file1-O file2