Emacs 和 gVim 打开不同编码的中文文件乱码的解决
附件:
文件注释: 适用于 gvim for Windows/Linux
vim-patch-zh-cn_windows_and_linux.zip [1.98 KiB]
被下载 53 次
方法:
* Emacs: 把 _emacs 放在 emacs 安装目录下,如
(Windows): d:\emacs-23.4\_emacs
或者
(Linux): ~/_emacs
* gVim: 把 _vimrc 放在 gvim 安装目录下,如
(Windows): C:\Program Files\Vim\_vimrc
作用:
for Emacs:
* 在 Windows/Linux 下打开 gb2312, gbk, gb18030, utf-8, unicode 编码的文件时,
文件内容,文件名均不会出现乱码;
Emacs 汉化包请从这里下载:
viewtopic.php?f=17&t=212467for gVim:
* 在 Windows/Linux 下打开 gb2312, gbk, utf-8,unicode编码的文件时,
文件内容,文件名均不会出现乱码;
* 在 Windows/Linux 下打开 gb18030 编码的文件,部分内容仍会出现乱码;
(原因是 VIM 对 GB18030仍未能完全支持,只是把它当成GBK来识别)
引用:
.emacs 文件内容示例:
;;; 把以下代码复制到 .emacs 文件
;; (或者 ~/_emacs, ~/.emacs.d/init.el 文件中效果相同)
;; 作用:可以解决 Emacs 对 UTF-8, UNICODE,GB2312,GB18030
;; 等中文内容和文件名乱码的问题)
;;; ---------------------
Emacs 22.3 及以往旧版本对多国语言支持不是太好,而且不支持 gb18030 中文编码, 从 23.1 开始才默认支持 gb18030。
解决方案:在 ~/.emacs 或 ~/_emacs 文件中新增或修改下面三项设置:
代码:
(set-language-environment 'Chinese-GB)
(setq-default pathname-coding-system 'euc-cn)
(setq file-name-coding-system 'euc-cn)
如果是 Emacs 22.3/Windows, 第一行请设为 Chinese-GB, 后面两行请设为 euc-cn;
如果是 Emacs 22.3/Linux, 第一行请设为 Chinese-GB, 后面两行请设为 utf-8;
如果是 Emacs 23.1/Windows 及更新的版本, 第一行建议设为 UTF-8 (设为 Chinese-GB 也行), 后面两行请设为 euc-cn;
如果是 Emacs 23.1/Linux 及更新的版本, 第一行建议设为 UTF-8 (设为 Chinese-GB 也行), 后面两行请设为 utf-8;
另外建议按下面的先后顺序来设置中文编码识别方式。
重要提示:写在最后一行的,实际上最优先使用; 最前面一行,反而放到最后才识别。utf-16le-with-signature 相当于 Windows 下的 Unicode 编码,这里也可写成
utf-16 (utf-16 实际上还细分为 utf-16le, utf-16be, utf-16le-with-signature
等多种)
代码:
;;; ---------------------
(prefer-coding-system 'cp950)
(prefer-coding-system 'gb2312)
(prefer-coding-system 'cp936)
(prefer-coding-system 'gb18030)
;(prefer-coding-system 'utf-16le-with-signature)
(prefer-coding-system 'utf-16)
(prefer-coding-system 'utf-8)
;;; ---------------------
_vimrc 文件内容:
代码:
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,utf-16,utf-32,latin-1