Vim 7の文字コードの設定

エンコーディングutf-8 のときのデフォルト(fileencodingsを指定していない場合)では、以下の順でエンコーディングのチェックを行います。

先頭のエンコーディングからチェックを行い、該当したものが、fileencodingに設定されます。ただし、ucs-bomの場合は、実際のエンコーディングの内容になります。Windowsレジストリファイルを開くと、ucs-2le になります。(Unicodeで、2バイト、リトルエンディアンを意味します)
このままだと、ucs-bomとutf-8に該当しない場合は、latin1になりますので、開く可能性があるエンコーディング名をそれ以前に追加します。

set encoding=utf-8
set fileencodings=ucs-bom,utf-8,iso-2022-jp,euc-jp,cp932,latin1

すべてのチェックに該当しなかった場合は、encodingだと判断され、fileencodingには何も設定されません。(ここではlatin1を設定していますので、必ず該当します)
オプション名は省略表記も可能です。

set enc=utf-8
set fencs=ucs-bom,utf-8,iso-2022-jp,euc-jp,cp932,latin1

VIM USER MANUAL - by Bram Moolenaar

45.4 Editing files with a different encoding

Suppose you have setup Vim to use Unicode, and you want to edit a file that is in 16-bit Unicode. Sounds simple, right? Well, Vim actually uses utf-8
encoding internally, thus the 16-bit encoding must be converted. Thus there is a difference between the character set (Unicode) and the encoding (utf-8 or 16-bit).
Vim will try to detect what kind of file you are editing. It uses the encoding names in the 'fileencodings' option. When using Unicode, the default value is: "ucs-bom,utf-8,latin1".
This means that Vim checks the file to see if it's one of these encodings:

  • ucs-bom File must start with a Byte Order Mark (BOM). This allows detection of 16-bit, 32-bit and utf-8 Unicode encodings.
  • utf-8 utf-8 Unicode. This is rejected when a sequence of bytes is illegal in utf-8.
  • latin1 The good old 8-bit encoding. Always works.

When you start editing that 16-bit Unicode file, and it has a BOM, Vim will detect this and convert the file to utf-8 when reading it. The 'fileencoding' option (without s at the end) is set to the detected value. In this case it is "ucs-2le". That means it's Unicode, two bytes and little-endian. This file format is common on MS-Windows (e.g., for registry files).
When writing the file, Vim will compare 'fileencoding' with 'encoding'. If they are different, the text will be converted.
An empty value for 'fileencoding' means that no conversion is to be done.Thus the text is assumed to be encoded with 'encoding'.
If the default 'fileencodings' value is not good for you, set it to the encodings you want Vim to try. Only when a value is found to be invalid will the next one be used. Putting "latin1" first doesn't work, because it is never illegal. An example, to fall back to Japanese when the file doesn't have a BOM and isn't utf-8:
:set fileencodings=ucs-bom,utf-8,sjis