我有一个以shift-JIS编码(日语)的html文件,在vim下无法阅读。设置enc=cp932或enc=sjis会生成垃圾字符。在emacs中,该文件看起来很好,所以我猜这是vim特定的问题。除了将其转换为像utf-8这样的合理编码之外,我该怎么办才能按原样读取它。
你不应该想要改变encoding
选项:它是用于字符串的内部表示,只有当当前编码不包含期望编码中存在的字符时才应更改。如果您有时使用sjis
编码编辑文件,则
请确保fileencodings
选项包含sjis
:将以下内容放入vimrc
中:
set fileencodings=ucs-bom,utf-8,sjis,default
e ++enc=sjis /path/to/file
打开您的文件。或者,如果文件已打开,请使用e! ++enc=sjis
(不带文件名)。来自vim帮助:
There are a few encodings which are similar, but not exactly the same. Vim
treats them as if they were different encodings, so that conversion will be
done when needed. You might want to use the similar name to avoid conversion
or when conversion is not possible:
cp932, shift-jis, sjis
cp936, euc-cn
对我来说,在Ubuntu Linux上,通过更改文件编码(++enc=),在gVim中工作得很好,但在Windows 7上却不行(所有字符都变成了?)。但我发现,即使不更改文件编码(默认的ucs-bom),只需更改guifont,就可以显示日语字符:
set guifont=MS_Gothic:h9:cSHIFTJIS
然而,输入日语字符仍然无法正常工作(使用Microsoft IME输入日语会出现?字符)。
仅供比较,在Linux上我的guifont只是:
guifont=Monospace 10
同时显示和输入(使用IBus Anthy输入法)都正常工作。