Home >Operation and Maintenance >Linux Operation and Maintenance >Introduction to solutions to garbled characters in Linux

Introduction to solutions to garbled characters in Linux

尚
Original
2019-12-07 15:36:173840browse

Introduction to solutions to garbled characters in Linux

When logging in to the Linux server remotely and using vim to edit and view files in the terminal, various Chinese garbled problems often occur.

The following settings can basically solve the vim Chinese garbled problem

First check the system’s support for Chinese

locale -a | grep zh_CN

The output sample is as follows

zh_CN.gbk

zh_CN.utf8

vim can only correctly recognize Chinese encoding files in the list, such as To recognize Chinese files of other encoding types, you need to upgrade the system

vi ~/.bash_profile

Add

export LANG="zh_CN.UTF at the end of the file -8"

export LC_ALL="zh_CN.UTF-8"

Online video tutorial recommendation: linux video tutorial

vim and Encoding-related parameters:

1. eencoding is used to configure the encoding of opening files and saving files, but it can only have one value. It is only suitable for environments where a few files have the same encoding, so it is generally not used

2. Fileencodings, as you can tell from the name, is an enhanced version of fileencoding. It can be configured with a variety of different encodings. The common configuration is that after configuration, as long as the text encoding in the list is legal, it can be used by vim For correct reading, it is recommended to configure: set fileencodings=utf-bom,utf-8,gbk,gb2312,gb18030,cp936,latin1

##3, encoding, vim internal encoding, after vim reads the file, but not It will not be processed by the encoding of the read file, but will be converted into an internal encoding format. This encoding is generally related to the operating system. Most of them are utf-8 under Linux, and gdk under Chinese windows. It is recommended to configure: set encoding=utf- 8

4. termencoding, the encoding of vim output. Output refers to output to the operating system or command terminal. The default is consistent with the language encoding of the operating system. If you use the linux command terminal, it is recommended that the terminal and the linux system have the same configuration. Encoding, and then configure the same termencoding, otherwise vim will not take care of the shell. However, if the shell does not have a Chinese name file, just configure the terminal and termencoding to be consistent. For windows, gbk and utf-8 can be automatically recognized, no need Special configuration, recommended configuration: set termencoding=utf-8

5, fileformats, used to distinguish operating systems, mainly the difference of carriage return\r\n, recommended configuration: set fileformats=unix,dos

Common garbled characters include the following situations:

(1) When rz files from Windows environment to Linux, the files appear garbled


Solution:

1. Use notepad before rz to convert the file format to UTF-8 BOM-less format or ANSI encoding format;

2. set encoding=utf-8;


(2) When garbled characters appear in the secureCRT or xterm2 editing environment, just adjust the character encoding to GB2312 or UTF-8 in the session options


(3) When comparing log files Garbled characters appear during vim editing. In most cases, it is because the format of the log file is GB2312.


Solution: 1.set encoding=GB2312; 2If solution 1 does not work, adjust the editing environment of secureCRT or xterm2 to GB2312


(4)wget The download file name is garbled


Solution: Generally add –restrict-file-names=nocontrol, for example wget --restrict-file-names=nocontrol -m www.xxx.com/


(5) The cat file is normal but the vim file is abnormal


Solution: Write directly to /etc/vim/vimrc and add

## to the last line

#set fileencodings=gb2312,utf-8

set fileencoding=gb2312

set termencoding=utf-8

The above is the detailed content of Introduction to solutions to garbled characters in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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