When opening a GBK file in Linux, it is found that Chinese will display garbled characters.This is because it is used under Linux When vim opens a file for editing, it will detect the encoding of the file one by one according to the character encoding of vim's fileencodings configuration.And set fileencoding to detect the correct character encoding method.If no suitable encoding is found, open it with latin-1 (ASCII) encoding (the last option).Use commands in vim:
:set fileencodings?View the current VIM editor fileencodings configuration value(Check other configurations such astermencoding, encoding is the same), after execution, it will display: fileencodings=ucs-bom, utf-8,latin1.In addition, you can view the configuration file of VIM: The first few lines of /etc/vimrc set this encoding order:
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$" /span>big5,latin -1 #This code is relatively complete, and the general one is fine.
There are the following four encoding configurations in vim: < /p>
encoding: The character encoding method used internally by Vim, including Vim's buffer (buffer), menu text, message text, etc.The user manual suggests to change its value only in.vimrc.In fact, it seems that it only makes sense to change its value in.vimrc.
fileencoding: The character encoding method of the file currently edited in Vim.When Vim saves the file, it will also save the file in this character encoding method (regardless of whether it is a new file All so).
fileencodings: When Vim starts, it will detect the character encoding of the file to be opened one by one according to the character encodings listed in it, and set fileencoding to the finally detected character encoding.Therefore, it is best to put the Unicode encoding method at the top of this list, and the Latin encoding method latin1 at the end.
termencoding: The character encoding method of the terminal (or the Console window of Windows) where Vim is working.This option is invalid for our commonly used GUI mode gVim under Windows, but for Vim in Console mode it is the code page of the Windows console, and usually we don't need to change it.If you use Vim in a terminal environment, you need to set the termencoding to be consistent with the encoding used by the terminal.For example:set termencoding=cp936 or set termencoding=utf-8
Vim’s multi-character encoding processing method:
(1) Vim is started, and the buffer, menu text, The character encoding method of the message text.
(2) Read the file to be edited, and detect the encoding of the file one by one according to the character encoding listed in fileencodings.And set fileencoding to detect the correct character encoding method.If no suitable encoding is found, open it with latin-1 (ASCII) encoding.
(3) Compare the values of fileencoding and encoding, if they are different, call iconv to convert the content of the file to the character encoding described by encoding, and put the converted content into the buffer opened for this file , Now we can start editing this file.
(4) When saving the file after editing, compare the values of fileencoding and encoding again.If it is different, call iconv again to convert the text in the buffer to be saved to the character encoding described by fileencoding and save it to the specified file.
(4) When saving the file after editing, compare the values of fileencoding and encoding again.If it is different, call iconv again to convert the text in the buffer to be saved to the character encoding described by fileencoding and save it to the specified file.
0 Comments