Vim character encoding settings

Source: Internet
Author: User

Setting of VIM encoding mode

Like all popular text editors, Vim can be very good at editing various character-encoded files, which of course includes popular Unicode encoding methods such as UCS-2, UTF-8, and so on. Unfortunately, as with many software from the Linux world, this requires you to set up your own hands.

  1. Vim has four options related to character encoding: encoding, fileencoding, Fileencodings, termencoding (the possible values for these options can be found in the VIM online Help: Helping Encoding-names ), they have the following meanings: The character encoding used internally by the
    • Encoding:vim, including the buffer of Vim, menu text, message text, and so on. The default is based on your locale selection. The user manual recommends changing its value only in. VIMRC, which in fact seems to only make sense if the value is changed in. vimrc. You can use another encoding to edit and save the file, such as your vim encoding for Utf-8, the edited file is cp936 encoded, VIM will automatically convert the file read into Utf-8 (Vim can read the way), and when you write to the file, It automatically turns back into cp936 (the file's save code). The character encoding of the file currently edited in the
    • Fileencoding:vim, and Vim saves the file as a character encoding (whether or not the new file is the case). The
    • Fileencodings:vim automatically probes the sequential list of fileencoding, which, when started, detects the character encoding of the file to be opened by the character encoding it lists, and fileencoding Set to the final detected character encoding method. It is therefore best to place Unicode encoding at the top of this list, Latin1 Latin encoding to the last side. The character encoding of the terminal (or the Console window of Windows) that the
    • Termencoding:vim works on. If Vim is in the same term as the VIM code, no setting is required. If not, you can use Vim's termencoding option to automatically convert to term encoding. This option is not valid for the gVim of our common GUI mode under Windows, and for the console mode vim is the Windows console code page, and Often we don't need to change it.
  2. Well, after explaining this pile of parameters that are easy for novices to confuse, let's look at how Vim's multi-character encoding support works.

    1. Vim starts and sets the character encoding for buffer, menu text, message text based on the value of encoding set in. vimrc. The
    2. reads the file that needs to be edited, probing the encoding of the file individually, based on the character encoding listed in Fileencodings. and set the fileencoding to be detected, it appears to be correct (note 1) the character encoding method.
    3. compares the values of fileencoding and encoding and, if different, calls Iconv to convert the contents of the file to the character encoding described by encoding, and places the converted content in the buffer opened for this file, at which point we can begin editing This file. Note that you need to call external Iconv.dll (note 2) to complete this step, and you need to ensure that the file exists in $VIMRUNTIME or other directory in the PATH environment variable.
    4. When you save the file after editing is complete, compare the values of fileencoding and encoding again. If it is different, call Iconv again to convert the text in the saved buffer to the character encoding described by fileencoding and save it to the specified file. Again, this requires calling Iconv.dll because Unicode can contain characters in almost all languages, and Unicode's UTF-8 encoding is a very cost-effective encoding (less space consuming than UCS-2), so it is recommended that the value of encoding be set to Utf-8 。 Another reason for this is that when encoding is set to Utf-8, the Vim Auto-detect file is encoded more accurately (perhaps this is the main;). We are editing files in Chinese Windows, in order to take into account the compatibility with other software, file encoding or set to GB2312/GBK more appropriate, so fileencoding recommended set to Chinese (Chinese is an individual name, in Unix represents gb2312 , which in Windows represents cp936, the code page for GBK).
  3. Configuration: For Fedora, the settings for Vim are generally placed in the/ETC/VIMRC file, but it is not recommended to modify it. You can modify the ~/.VIMRC file (which does not exist by default, you can create a new one yourself) and write the desired settings.

  4. My. vimrc file is as follows:

set encoding=utf-8set fileencodings=ucs-bom,utf-8,cp936set termencoding=utf-8

Where the fileencoding configuration can set Utf-8, but my mp3 does not seem to support utf-8 encoding, so simply, I set it to gb2312. Now it is done, whether it is VI or MP3 can be displayed without garbled. txt files.

    1. Reference

Http://www.cnblogs.com/freewater/archive/2011/08/26/2154602.html

Vim character encoding settings

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.