Emacs和gVim中文乱码解决方案[2013-4-8更新]

Vim、Emacs配置和使用
回复
头像
careone
帖子: 839
注册时间: 2007-12-17 21:41

Emacs和gVim中文乱码解决方案[2013-4-8更新]

#1

帖子 careone » 2012-08-01 17:24

Emacs 和 gVim 打开不同编码的中文文件乱码的解决
vim-patch-zh-cn_windows_and_linux.zip
适用于 gvim for Windows/Linux
(1.98 KiB) 已下载 165 次
方法:
* 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 汉化包请从这里下载:
http://forum.ubuntu.org.cn/viewtopic.php?f=17&t=212467


for 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
上次由 careone 在 2013-04-08 21:59,总共编辑 5 次。
魁拔不死,天下不宁。
魁拔既死,天下不平。
--------------
Careone <emacslocale^126.com>
https://sourceforge.net/projects/emacslocale/files/
gy619
帖子: 237
注册时间: 2011-10-11 10:16

Re: Emacs 和 gVim 打开不同编码的中文文件乱码的解决

#2

帖子 gy619 » 2012-08-01 19:11

现在新版本的emacs,看中文根本不会有乱码,包括gbk,gb2312等等格式。

不过,在emacs上面,将gb2312 转成 utf8,我每次转换都有乱码。
头像
careone
帖子: 839
注册时间: 2007-12-17 21:41

Re: Emacs 和 gVim 打开不同编码的中文文件乱码的解决

#3

帖子 careone » 2012-08-02 14:08

gy619 写了:现在新版本的emacs,看中文根本不会有乱码,包括gbk,gb2312等等格式。

不过,在emacs上面,将gb2312 转成 utf8,我每次转换都有乱码。
新版本指 Emacs 24.1 吗?

一般 Emacs 在 Linux 上乱码出现少点(包括文件名,文件内容两方面)。
但 Emacs 在 Windows 上乱码就很常见了。
魁拔不死,天下不宁。
魁拔既死,天下不平。
--------------
Careone <emacslocale^126.com>
https://sourceforge.net/projects/emacslocale/files/
gy619
帖子: 237
注册时间: 2011-10-11 10:16

Re: Emacs 和 gVim 打开不同编码的中文文件乱码的解决

#4

帖子 gy619 » 2012-08-02 17:48

careone,

我用的是ubuntu。没有在windows下面用过emacs。

我现在用开发版,就是大概一周左右就升级一次的emacs的。我现在的版本是24.1.50.1。

用emacs 23版本的时候,我发现看gbk,gb2312这类的格式就要乱码。

升级到24版本,乱码从此没有遇见过。
回复