Original address: http://www.cnblogs.com/joeyupdo/archive/2013/03/03/2941737.html
-----------
In vim to edit a Chinese text, sometimes seems to have garbled, before are tinkering to get no garbled, the problem has always been bothering me. Suddenly think of this problem, so want to solve it, in the online search for a long time, there are a lot of this information, but not very in place, after a day of tossing, will vim uninstall a lot of times, also installed many times, and did some small test, today finally finished.
1, download
Download the latest 7.3 version of Vim source to http://www.vim.org/.
2, installation
Before compiling, you need to./configure--help Check the configuration options,
This configuration needs to be added in:--enable-multibyte Include multibyte editing support
The role is to support multibyte encodings, which should be important. Otherwise, the configuration seems to be useless, I do not know if I say the right ^_^.
After the configuration is ready, nature is normal: make, made install
3, the last is the VIMRC script configuration
Edit the ~/.VIMRC file, plus the following lines:
Set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
Set Termencoding=utf-8
Set Encoding=utf-8
can be done.
By the way, write a detailed introduction to Vim code.
Vim, like all popular text editors, is a good way to edit a variety of character-encoded files, including, of course, popular Unicode encodings such as UCS-2 and UTF-8.
Vim has four options related to character encoding, encoding, fileencoding, fileencodings, termencoding (these options may be taken, please refer to vim online Help: Helping Encoding-names), Their significance is as follows:
(1) Encoding:vim internal use of the character encoding method, including Vim buffer (buffers), menu text, message text and so on. The user's manual suggests changing its value only in. VIMRC, and in fact it seems only meaningful to change its value in. vimrc.
(2) The character encoding of the file currently edited in Fileencoding:vim, Vim saves the file as this character encoding (whether or not the new file is the case), the Internet is introduced, But I did it in. VIMRC defined as Utf-8 seems to have no effect, only when the vim file open the manual settings will be effective, do not know why.
(3) When Fileencodings:vim starts, it detects the character encoding of the file that will be opened, and sets the fileencoding to the final detected character encoding, according to the character encoding it lists. So it's best to put the Unicode encoding at the top of this list, and put the Latin coding latin1 on the last side.
(4) The character encoding of the terminal (or Windows Console window) that the Termencoding:vim is working on. This option is not valid for the gVim of our commonly used GUI schemas under Windows, and for Vim in console mode is the code page for the Windows console, and usually we don't need to change it.
Let's take a look at how Vim's multiple character encoding support works.
(1) Vim starts by setting buffer, menu text, and character encoding for the message text based on the value of the encoding set in the. vimrc.
(2) Read the file that needs to be edited, and probe the encoding of the file each way according to the character encoding listed in Fileencodings. and set fileencoding to detect what appears to be the correct character encoding, and if no appropriate encoding is found, open with latin-1 (ASCII) encoding.
(3) Compare the values of fileencoding and encoding, if different then invoke Iconv to convert the file contents to the character encoding described in encoding, and put the converted content into the buffer for this file, we can start editing this file now.
(4) When the editor is finished saving the file, compare the values of fileencoding and encoding again. If different, call Iconv to convert the text in the upcoming buffer to the character encoding described by fileencoding and save it to the specified file.
Because Unicode can contain almost all of the language's characters, and Unicode's UTF-8 encoding is a very cost-effective way to encode (space consumption is smaller than UCS-2), the recommended encoding value is set to Utf-8. Another reason to do this is that Vim automatically detects files in a more accurate way when encoding is set to Utf-8 (perhaps the main;). The files we edited in Chinese Windows, in order to accommodate compatibility with other software, the file encoding or set to GB2312/GBK is more appropriate, so the fileencoding recommendation is set to Chinese (Chinese is an individual name, in the Unix representation gb2312 , which represents cp936 in Windows, which is the code page for GBK.