Occasionally we will encounter vim when opening a file garbled, this is likely to be the encoding problem, the default file format in Windows is GBK (gb2312), and Linux is generally a UTF-8. UTF-8, big5, and gb2312 are supported. So how do we view the encoding and how to proceed in Linux...
Occasionally we will encounter vim when opening a file garbled, this is likely to be the encoding problem, the default file format in Windows is GBK (gb2312), and Linux is generally a UTF-8.
UTF-8, big5, and gb2312 are supported. So how do we view the encoding in Linux and how do we convert the file encoding?
1. view encoding
1. check the default supported encoding:
[Root @ localhost ~] # Cat/etc/sysconfig/i18n
LANG = "zh_CN.UTF-8"
SYSFONT = "lat0-sun16"
2. view the terminal interface code:
This is related to the LANG variable:
[Root @ localhost ~] # Echo $ LANG
Zh_CN.UTF-8
3. view the file encoding.
You can view the file encoding in Linux in the following ways:
1) you can directly view the file encoding in Vim.
: Set fileencoding
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:
Set encoding = UTF-8 fileencodings = ucs-bom, UTF-8, big5, gb2312, cp936, latin1
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
Generally, as long as the software that opens the terminal interface is consistent with the file encoding, garbled code can be avoided.
II. file encoding and conversion
1. convert the file encoding directly in Vim. for example, convert a file to UTF-8 format.
: Sets 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 original encoding-t new encoding filename
For example, converting a UTF-8-encoded file into GBK encoding
[Root @ localhost ~] # Iconv-f cp936-t UTF-8-o appops.txt
-O: creates a new encoding file and retains the source file and its original encoding.
This article is from the "learning notes" blog