Vim Learning Log (8): Linux View and modify file encoding

Source: Internet
Author: User



To view the encoding of a file



Method One:



1. The file encoding can be viewed directly in vim
: Set fileencoding
You can display the file encoding format.
Note: If you just want to see other encoded files or if you want to solve the problem of viewing files garbled with Vim, you can
Add the following to the ~/.VIMRC file:
Set Encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936



2.enca (if this command is not installed on your system, you can use sudo yum install-y eNCA installation) to view the file encoding
$ enca filename
Filename:universal Transformation Format 8 bits; UTF-8
CRLF Line Terminators
It is important to note that eNCA is not very good at identifying certain GBK encoded files and will appear when identified:
Unrecognized encoding



Method Two: (Convenient)



Linux commands:



File File name



Display the format of a file



File Encoding Conversion
1. Convert file encoding directly into Vim, such as converting a file to Utf-8 format (convenient)
: Set Fileencoding=utf-8



2. Enconv conversion file encoding, such as 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
such as converting a UTF-8 encoded file into a GBK encoding.
Iconv-f UTF-8-T GBK file1-o file2



Second, file name encoding conversion

Because now using linux, the original files in windows are encoded in GBK. So copy to linux is garbled, the file content can be converted using iconv, but a lot of Chinese file names or garbled, find a command that can convert file name encoding is convmv.
 

convmv command detailed parameters e.g.
 

convmv -f GBK -t UTF-8 * .mp3
 

However, this command does not convert straight, you can see the contrast before and after the conversion. If you want a straight conversion, you need to add the parameter --notest
 

convmv -f GBK -t UTF-8 --notest * .mp3
 

The -f parameter indicates the encoding before conversion, and -t indicates the encoding after conversion. Don't get this wrong. Otherwise, it may still be garbled. One more parameter is useful. It is -r which means recursively transform all subdirectories under the current directory.
 

* Requires convmv-1.10-1.el5.noarch.rpm
Third, better enca command line tool enca, it can not only intelligently identify the encoding of the file, but also supports batch conversion.
1. Installation
$ sudo apt-get install enca
2. View the current file encoding
enca -L zh_CN ip.txt Simplified Chinese National Standard; GB2312 Surrounded by / intermixed with non-text data
3.Conversion Command format is as follows
$ enca -L current language -x target encoding file name
For example, to convert all files in the current directory to UTF-8
enca -L zh_CN -x utf-8 *
Check file encoding enca -L zh_CN file
 Convert file encoding to "UTF-8" encoding enca -L zh_CN -x UTF-8 file
If you don't want to overwrite the original file, you can do this enca -L zh_CN -x UTF-8 <file1> file2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.