Enable vim to open the gbk file in UTF-8 local

Source: Internet
Author: User
~ /. Vimrc to add setfencsutf-8, gbk this line is to tell vim, open a file, try utf8, gbk two types of encoding, vim only need to scan the file before paragraph, you can determine whether the file uses UTF-8 or gbk encoding based on the data in the file. If not ...~ /. Add vimrc
Set fencs = UTF-8, gbk
 
This line tells vim to try utf8 and gbk encoding when opening a file. vim only
Before scanning a file, you can determine whether the file is used based on the data in the file.
UTF-8 or gbk encoding. If this line is not specified, vim only uses the current encoding (locale)
To open the file, because your locale is a UTF-8, and the file is gbk, so opening is garbled.
 
 
Bytes ----------------------------------------------------------------------------------------------------
 
Generally, garbled characters may occur when vim opens a Chinese File. The reason is complicated and it is not so cool. Direct solution
 
Set fileencoding = gb18030
Set fileencodings = UTF-8, gb18030, UTF-16, big5
 
Do you want to see the reason for this setting? Continue. The following is widely used in the Internet
 
The encoding in vim is mainly related to three parameters: enc (encoding), fenc (fileencoding), and fencs (fileencodings)
 
Fenc indicates the encoding of the current file, that is, a file correctly displayed in vim (provided that your system environment matches your enc settings ), you can change fenc and then w to save the file into different encodings. For example, I: set fenc = UTF-8 and then: w will save the file as UTF-8, and: set fenc = gb18030 and then: w will save the file as gb18030. This value does not matter whether the file can be correctly decoded when it is opened.
 
Fencs is a list of guesses used to decode a file when it is opened. File encoding is not correct, so vim can only guess the file encoding. For example, the setting in my vimrc is
 
Set fileencodings = UTF-8, gb18030, UTF-16, big5
 
Therefore, each time I open a file in vim, I first try to use UTF-8 for decoding. If an error occurs when I use UTF-8 for decoding, the so-called error means that UTF-8 cannot be correctly decoded somewhere ), then, use gb18030 to re-decode the code from the beginning. if gb18030 is wrong (note that gb18030 is not a UTF-8-like rule encoding, the so-called error is that a specific encoding has no meaningful words, for example 0), try to use the UTF-16, still try to use big5 error. In this case, if there is no error in the decoding process from the beginning to the end, vim considers the file to be encoded and will not try again. At this time, the fenc value will be set as the final encoding value used by vim. you can use: set fenc? To view the specific information.
 
Of course, this may also cause errors. for example, if your file is gb18030 encoded, but in fact only one or two characters are Chinese, then they may also be decoded by UTF-8, this file will be mistakenly identified as UTF-8, resulting in incorrect decoding.
 
As for enc, its function is basically display. No matter what encoding the final file is, vim will convert it to the current system encoding for processing, so that it can be correctly displayed in the current system, therefore, enc does this. In windows, the default value of enc is cp936, which is the default encoding of Chinese windows. Therefore, enc does not need to be changed. In linux, as your system locale may be set to zh_CN.gb18030 or zh_CN.utf-8, your enc should be set to gb18030 or UTF-8 (or gbk ).
 
Finally, let's talk about the default encoding of the new empty file. It seems that the first encoding in fencs will be used as the default encoding of the new file. However, there is a problem here, that is, the order of fencs has a lot to do with the decoding success rate. according to my experience, the success rate of UTF-8 is higher than that of gb18030, if I create a new file and want it to be gb18030 encoded by default, what should I do? One method is to set fenc = gb18030 after each file creation, but I found that setting fenc = gb18030 in vimrc can also achieve this effect.
 
In addition, some people have proposed this method in the ubuntu Chinese forum and configured it directly.
 
Paste all the code directly to the terminal to run it!
Installer
Code:
Sudo apt-get install vim-gtk vim-doc cscope
 
Create a startup item
Code:
 
Cat>/usr/share/applications/gvim. desktop <"EOF"
[Desktop Entry]
Name = Gvim
Comment [zh_CN] = Gvim editor
Exec = gvim
Icon =/usr/share/pixmaps/gnome-word.png
Terminal = false
X-MultipleArgs = false
Type = Application
Categories = Application; Development;
Encoding = UTF-8
StartupNotify = true
EOF
 
Locale is the configuration file zh_CN.utf8
Code:
 
Cat> $ HOME/. vimrc <"EOF"
"============================================== ==========================================
"Project: gvim configuration file
"Author: yonsan [QQ: 82555472]
"Installation: sudo apt-get install vim-gtk
"Usage: copy this file (. vimrc) to $ HOME/
"============================================== ==========================================
 
"Use the murphy palette
Colo murphy
"Set the font list for the GUI.
Set guifont = SimSun 10
"
Set nocompatible
"Set the file browser directory to the current directory
Set bsdir = buffer
Set autochdir
"Set encoding
Set enc = UTF-8
"Set file encoding
Set fenc = UTF-8
"Sets the file encoding detection type and Supported formats
Set fencs = UTF-8, ucs-bom, gb18030, gbk, gb2312, cp936
"Specify the menu language
Set langmenu = zh_CN.UTF-8
Source $ VIMRUNTIME/delmenu. vim
Source $ VIMRUNTIME/menu. vim
"Set high syntax brightness
Set syn = cpp
"Display row number
Set nu!
"High brightness display of search results
Set hlsearch
"Tab width
Set tabstop = 4
Set cindent shiftwidth = 4
Set autoindent shiftwidth = 4
"C/C ++ comments
Set comments = ://
"Corrected the automatic C style annotation function <2005/07/16>
Set comments = s1:/*, mb: *, ex0 :/
"Enhanced search functions
Set tags =./tags,./../tags,./**/tags
"Save File format
Set fileformats = unix, dos
"Keyboard operations
Map gk
Map gj
"Command line height
Set partition height = 1
"Using cscope
If has ("CSAs ")
Set csprg =/usr/bin/cs.pdf
Set csto = 0
Set cst
Set nocsverb
"Add any database in current directory
If filereadable ("cscope. out ")
Cs add cscope. out
"Else add database pointed to by environment
Elseif $ CSCOPE_DB! = ""
Cs add $ CSCOPE_DB
Endif
Set csverb
Endi
"Chinese help
If version> 603
Set helplang = cn
Endi
EOF
 
Configuration file where locale is zh_CN.gbk
Code:
 
Cat> $ HOME/. vimrc <"EOF"
"============================================== ==========================================
"Project: gvim configuration file
"Author: yonsan [QQ: 82555472]
"Installation: sudo apt-get install vim-gtk
"Usage: copy this file (. vimrc) to $ HOME/
"============================================== ==========================================
 
"Use the murphy palette
Colo murphy
"Set the font list for the GUI.
Set guifont = SimSun 10
"
Set nocompatible
"Set the file browser directory to the current directory
Set bsdir = buffer
Set autochdir
"Set encoding
Set enc = chinese
"Set file encoding
Set fenc = chinese
"Sets the file encoding detection type and Supported formats
Set fencs = gbk, UTF-8, ucs-bom, gb18030, gb2312, cp936
"Specify the menu language
Set langmenu = zh_CN.GBK
Source $ VIMRUNTIME/delmenu. vim
Source $ VIMRUNTIME/menu. vim
"Set high syntax brightness
Set syn = cpp
"Display row number
Set nu!
"High brightness display of search results
Set hlsearch
"Tab width
Set tabstop = 4
Set cindent shiftwidth = 4
Set autoindent shiftwidth = 4
"C/C ++ comments
Set comments = ://
"Corrected the automatic C style annotation function <2005/07/16>
Set comments = s1:/*, mb: *, ex0 :/
"Enhanced search functions
Set tags =./tags,./../tags,./**/tags
"Save File format
Set fileformats = unix, dos
"Keyboard operations
Map gk
Map gj
"Command line height
Set partition height = 1
"Using cscope
If has ("CSAs ")
Set csprg =/usr/bin/cs.pdf
Set csto = 0
Set cst
Set nocsverb
"Add any database in current directory
If filereadable ("cscope. out ")
Cs add cscope. out
"Else add database pointed to by environment
Elseif $ CSCOPE_DB! = ""
Cs add $ CSCOPE_DB
Endif
Set csverb
Endi
"Chinese help
If version> 603
Set helplang = cn
Endi
EOF


From AlphaJay's blog
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.