vim encoding and font

from:

http://www.pmme.cn/archives/the-encoding-of-vim/


一般的,vim打开中文文件时会出现乱码,原因比较复杂,不罗嗦了。直接讲解决办法

    set fileencoding=gb18030
    set fileencodings=utf-8,gb18030,utf-16,big5

想看这样设置的原因吗?请继续。下文在网络中广泛流传

vim里面的编码主要跟三个参数有关:enc(encoding), fenc(fileencoding)和fencs(fileencodings)

其中fenc是当前文件的编码,也就是说,一个在vim里面已经正确显示了的文件(前提是你的系统环境跟你的enc设置匹配),你可以通过改变 fenc后再w来将此文件存成不同的编码。比如说,我:set fenc=utf-8然后:w就把文件存成utf-8的了,:set fenc=gb18030再:w就把文件存成gb18030的了。这个值对于打开文件的时候是否能够正确地解码没有任何关系。

fencs就是用来在打开文件的时候进行解码的猜测列表。文件编码没有百分百正确的判断方法,所以vim只能猜测文件编码。比如我的vimrc里面这个的设置是

set fileencodings=utf-8,gb18030,utf-16,big5

所以我的vim每打开一个文件,先尝试用utf-8进行解码,如果用utf-8解码到了一半出错(所谓出错的意思是某个地方无法用utf-8正确地解码),那么就从头来用gb18030重新尝试解码,如果gb18030又出错(注意gb18030并不是像utf-8似的规则编码,所以所谓的出错只是说某个编码没有对应的有意义的字,比如0),就尝试用utf-16,仍然出错就尝试用big5。这一趟下来,如果中间的某次解码从头到尾都没有出错,那么 vim就认为这个文件是这个编码的,不会再进行后面的尝试了。这个时候,fenc的值就会被设为vim最后采用的编码值,可以用:set fenc?来查看具体是什么。

当然这个也是有可能出错的,比如你的文件是gb18030编码的,但是实际上只有一两个字符是中文,那么有可能他们正好也能被utf-8解码,那么这个文件就会被误认为是utf-8的导致错误解码。

至于enc,其作用基本只是显示。不管最后的文件是什么编码的,vim都会将其转换为当前系统编码来进行处理,这样才能在当前系统里面正确地显示出来,因此enc就是干这个的。在windows下面,enc默认是cp936,这也就是中文windows的默认编码,所以enc是不需要改的。在 linux下,随着你的系统locale可能设为zh_CN.gb18030或者zh_CN.utf-8,你的enc要对应的设为gb18030或者 utf-8(或者gbk之类的)。

最后再来说一下新建空文件的默认编码。看文档好像说会采用fencs里面的第一个编码作为新建文件的默认编码。但是这里有一个问题,就是fencs 的顺序跟解码成功率有很大关系,根据我的经验utf-8在前比gb18030在前成功率要高一些,那么如果我新建文件默认想让它是gb18030编码怎么办?一个方法是每次新建文件后都:set fenc=gb18030一下,不过我发现在vimrc里面设置fenc=gb18030也能达到这个效果。

另外,在ubuntu中文论坛还有人提出了这样的办法,直接就配置了

    所有代码直接粘贴到终端运行即可!
    安装程序
    代码:
    sudo apt-get install vim-gtk vim-doc cscope

    创建启动项
    代码:

    cat > /usr/share/applications/gvim.desktop < $HOME/.vimrc << “EOF”
    “===========================================================================
    ” 项目: gvim 配置文件
    ” 作者: yonsan [QQ:82555472]
    ” 安装: sudo apt-get install vim-gtk
    ” 用法: 将本文件(.vimrc)拷贝到$HOME/
    “===========================================================================

    ” 使用 murphy 调色板
    colo murphy
    ” 设置用于GUI图形用户界面的字体列表。
    set guifont=SimSun 10
    ”
    set nocompatible
    ” 设定文件浏览器目录为当前目录
    set bsdir=buffer
    set autochdir
    ” 设置编码
    set enc=utf-8
    ” 设置文件编码
    set fenc=utf-8
    ” 设置文件编码检测类型及支持格式
    set fencs=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
    ” 指定菜单语言
    set langmenu=zh_CN.UTF-8
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim
    ” 设置语法高亮度
    set syn=cpp
    “显示行号
    set nu!
    ” 查找结果高亮度显示
    set hlsearch
    ” tab宽度
    set tabstop=4
    set cindent shiftwidth=4
    set autoindent shiftwidth=4
    ” C/C++注释
    set comments=://
    ” 修正自动C式样注释功能
    set comments=s1:/*,mb:*,ex0:/
    ” 增强检索功能
    set tags=./tags,./../tags,./**/tags
    ” 保存文件格式
    set fileformats=unix,dos
    ” 键盘操作
    map gk
    map gj
    ” 命令行高度
    set cmdheight=1
    ” 使用cscope
    if has(”cscope”)
    set csprg=/usr/bin/cscope
    set csto=0
    set cst
    set nocsverb
    ” add any database in current directory
    if filereadable(”cscope.out”)
    cs add cscope.out
    ” else add database pointed to by environment
    elseif $CSCOPE_DB != “”
    cs add $CSCOPE_DB
    endif
    set csverb
    endi
    ” 中文帮助
    if version > 603
    set helplang=cn
    endi
    EOF

    locale为zh_CN.gbk的配置文件
    代码:

    cat > $HOME/.vimrc << “EOF”
    “===========================================================================
    ” 项目: gvim 配置文件
    ” 作者: yonsan [QQ:82555472]
    ” 安装: sudo apt-get install vim-gtk
    ” 用法: 将本文件(.vimrc)拷贝到$HOME/
    “===========================================================================

    ” 使用 murphy 调色板
    colo murphy
    ” 设置用于GUI图形用户界面的字体列表。
    set guifont=SimSun 10
    ”
    set nocompatible
    ” 设定文件浏览器目录为当前目录
    set bsdir=buffer
    set autochdir
    ” 设置编码
    set enc=chinese
    ” 设置文件编码
    set fenc=chinese
    ” 设置文件编码检测类型及支持格式
    set fencs=gbk,utf-8,ucs-bom,gb18030,gb2312,cp936
    ” 指定菜单语言
    set langmenu=zh_CN.GBK
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim
    ” 设置语法高亮度
    set syn=cpp
    “显示行号
    set nu!
    ” 查找结果高亮度显示
    set hlsearch
    ” tab宽度
    set tabstop=4
    set cindent shiftwidth=4
    set autoindent shiftwidth=4
    ” C/C++注释
    set comments=://
    ” 修正自动C式样注释功能
    set comments=s1:/*,mb:*,ex0:/
    ” 增强检索功能
    set tags=./tags,./../tags,./**/tags
    ” 保存文件格式
    set fileformats=unix,dos
    ” 键盘操作
    map gk
    map gj
    ” 命令行高度
    set cmdheight=1
    ” 使用cscope
    if has(”cscope”)
    set csprg=/usr/bin/cscope
    set csto=0
    set cst
    set nocsverb
    ” add any database in current directory
    if filereadable(”cscope.out”)
    cs add cscope.out
    ” else add database pointed to by environment
    elseif $CSCOPE_DB != “”
    cs add $CSCOPE_DB
    endif
    set csverb
    endi
    ” 中文帮助
    if version > 603
    set helplang=cn
    endi

    EOF

     

from:

http://www.xxlinux.com/linux/e/DoPrint/?classid=5&id=9171

 

和所有的流行文本编辑器一样,Vim 可以很好的编辑各种字符编码的文件,这当然包括UCS-2、UTF-8 等流行的 Unicode 编码方式。然而不幸的是,和很多来自 Linux 世界的软件一样,这需要你自己动手设置。

Vim 有四个跟字符编码方式有关的选项,encoding、fileencoding、fileencodings、termencoding (这些选项可能的取值请参考 Vim 在线帮助 :help encoding-names),它们的意义如下:

* encoding: Vim 内部使用的字符编码方式,包括 Vim 的 buffer (缓冲区)、菜单文本、消息文本等。默认是根据你的locale选择.用户手册上建议只在 .vimrc 中改变它的值,事实上似乎也只有在.vimrc 中改变它的值才有意义。你可以用另外一种编码来编辑和保存文件,如你的vim的encoding为utf-8,所编辑的文件采用cp936编码,vim会 自动将读入的文件转成utf-8(vim的能读懂的方式),而当你写入文件时,又会自动转回成cp936(文件的保存编码).

* fileencoding: Vim 中当前编辑的文件的字符编码方式,Vim 保存文件时也会将文件保存为这种字符编码方式 (不管是否新文件都如此)。

* fileencodings: Vim自动探测fileencoding的顺序列表, 启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的字符编码方式,并且将 fileencoding 设置为最终探测到的字符编码方式。因此最好将Unicode 编码方式放到这个列表的最前面,将拉丁语系编码方式 latin1 放到最后面。

* termencoding: Vim 所工作的终端 (或者 Windows 的 Console 窗口) 的字符编码方式。如果vim所在的term与vim编码相同,则无需设置。如其不然,你可以用vim的termencoding选项将自动转换成term 的编码.这个选项在 Windows 下对我们常用的 GUI 模式的 gVim 无效,而对 Console 模式的Vim 而言就是 Windows 控制台的代码页,并且通常我们不需要改变它。

好了,解释完了这一堆容易让新手犯糊涂的参数,我们来看看 Vim 的多字符编码方式支持是如何工作的。

1. Vim 启动,根据 .vimrc 中设置的 encoding 的值来设置 buffer、菜单文本、消息文的字符编码方式。

2. 读取需要编辑的文件,根据 fileencodings 中列出的字符编码方式逐一探测该文件编码方式。并设置 fileencoding 为探测到的,看起来是正确的 (注1) 字符编码方式。

3. 对比 fileencoding 和 encoding 的值,若不同则调用 iconv 将文件内容转换为encoding 所描述的字符编码方式,并且把转换后的内容放到为此文件开辟的 buffer 里,此时我们就可以开始编辑这个文件了。注意,完成这一步动作需要调用外部的 iconv.dll(注2),你需要保证这个文件存在于 $VIMRUNTIME 或者其他列在 PATH 环境变量中的目录里。

4. 编辑完成后保存文件时,再次对比 fileencoding 和 encoding 的值。若不同,再次调用 iconv 将即将保存的 buffer 中的文本转换为 fileencoding 所描述的字符编码方式,并保存到指定的文件中。同样,这需要调用 iconv.dll由于 Unicode 能够包含几乎所有的语言的字符,而且 Unicode 的 UTF-8 编码方式又是非常具有性价比的编码方式 (空间消耗比 UCS-2 小),因此建议 encoding 的值设置为utf-8。这么做的另一个理由是 encoding 设置为 utf-8 时,Vim 自动探测文件的编码方式会更准确 (或许这个理由才是主要的 ;)。我们在中文 Windows 里编辑的文件,为了兼顾与其他软件的兼容性,文件编码还是设置为 GB2312/GBK 比较合适,因此 fileencoding 建议设置为 chinese (chinese 是个别名,在 Unix 里表示 gb2312,在 Windows 里表示cp936,也就是 GBK 的代码页)。

以 下是我的 .vimrc(见附件) 中关于字符编码方式设置的内容,这个设置比较有弹性,可以根据系统中的环境变量 $LANG (当然,Windows 中的写法是 %LANG%) 的值来自动设置合适的字符编码方式。此时,推荐设置 %LANG% = zh_CN.UTF-8,可以通过后面的 Windows 注册表脚本文件来方便的做到。

注1: 事实上,Vim 的探测准确度并不高,尤其是在 encoding 没有设置为 utf-8 时。因此强烈建议将 encoding 设置为 utf-8,虽然如果你想 Vim 显示中文菜单和提示消息的话这样会带来另一个小问题。

注2: 在 GNU 的 FTP 上可以下载到 iconv 的 Win32 版(http://mirrors.kernel.org/gnu/libiconv/libiconv-1.9.1.bin.woe32.zip),不 推荐去GnuWin32(http://gnuwin32.sourceforge.net/) 下载 libiconv,因为那个版本旧一些,并且需要自己改名 dll 文件。

注3: 查看帮助 :h iconv-dynamic

On MS-Windows Vim can be compiled with the |+iconv/dyn| feature. This means

Vim will search for the "iconv.dll" and "libiconv.dll" libraries. When

neither of them can be found Vim will still work but some conversions won't be

possible.

附1:vimrc文件

" Multi-encoding setting, MUST BE IN THE BEGINNING OF .vimrc!
"
if has("multi_byte")
" When 'fileencodings' starts with 'ucs-bom', don't do this manually
"set bomb
set fileencodings=ucs-bom,chinese,taiwan,japan,korea,utf-8,latin1
" CJK environment detection and corresponding setting
if v:lang =~ "^zh_CN"
" Simplified Chinese, on Unix euc-cn, on MS-Windows cp936
set encoding=chinese
set termencoding=chinese
if &fileencoding == ''
set fileencoding=chinese
endif
elseif v:lang =~ "^zh_TW"
" Traditional Chinese, on Unix euc-tw, on MS-Windows cp950
set encoding=taiwan
set termencoding=taiwan
if &fileencoding == ''
set fileencoding=taiwan
endif
elseif v:lang =~ "^ja_JP"
" Japanese, on Unix euc-jp, on MS-Windows cp932
set encoding=japan
set termencoding=japan
if &fileencoding == ''
set fileencoding=japan
endif
elseif v:lang =~ "^ko"
" Korean on Unix euc-kr, on MS-Windows cp949
set encoding=korea
set termencoding=korea
if &fileencoding == ''
set fileencoding=korea
endif
endif
" Detect UTF-8 locale, and override CJK setting if needed
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set encoding=utf-8
endif
else
echoerr 'Sorry, this version of (g)Vim was not compiled with "multi_byte"'
endif

附2:

Supported 'encoding' values are:                        *encoding-values*
1 latin1 8-bit characters (ISO 8859-1)
1 iso-8859-n ISO_8859 variant (n = 2 to 15)
1 koi8-r Russian
1 koi8-u Ukrainian
1 macroman MacRoman (Macintosh encoding)
1 8bit-{name} any 8-bit encoding (Vim specific name)
1 cp437 similar to iso-8859-1
1 cp737 similar to iso-8859-7
1 cp775 Baltic
1 cp850 similar to iso-8859-4
1 cp852 similar to iso-8859-1
1 cp855 similar to iso-8859-2
1 cp857 similar to iso-8859-5
1 cp860 similar to iso-8859-9
1 cp861 similar to iso-8859-1
1 cp862 similar to iso-8859-1
1 cp863 similar to iso-8859-8
1 cp865 similar to iso-8859-1
1 cp866 similar to iso-8859-5
1 cp869 similar to iso-8859-7
1 cp874 Thai
1 cp1250 Czech, Polish, etc.
1 cp1251 Cyrillic
1 cp1253 Greek
1 cp1254 Turkish
1 cp1255 Hebrew
1 cp1256 Arabic
1 cp1257 Baltic
1 cp1258 Vietnamese
1 cp{number} MS-Windows: any installed single-byte codepage
2 cp932 Japanese (Windows only)
2 euc-jp Japanese (Unix only)
2 sjis Japanese (Unix only)
2 cp949 Korean (Unix and Windows)
2 euc-kr Korean (Unix only)
2 cp936 simplified Chinese (Windows only)
2 euc-cn simplified Chinese (Unix only)
2 cp950 traditional Chinese (on Unix alias for big5)
2 big5 traditional Chinese (on Windows alias for cp950)
2 euc-tw traditional Chinese (Unix only)
2 2byte-{name} Unix: any double-byte encoding (Vim specific name)
2 cp{number} MS-Windows: any installed double-byte codepage
u utf-8 32 bit UTF-8 encoded Unicode (ISO/IEC 10646-1)
u ucs-2 16 bit UCS-2 encoded Unicode (ISO/IEC 10646-1)
u ucs-2le like ucs-2, little endian
u utf-16 ucs-2 extended with double-words for more characters
u utf-16le like utf-16, little endian
u ucs-4 32 bit UCS-4 encoded Unicode (ISO/IEC 10646-1)
u ucs-4le like ucs-4, little endian

The {name} can be any encoding name that your system supports. It is passed
to iconv() to convert between the encoding of the file and the current locale.
For MS-Windows "cp{number}" means using codepage {number}.

Several aliases can be used, they are translated to one of the names above.
An incomplete list:

1 ansi same as latin1 (obsolete, for backward compatibility)
2 japan Japanese: on Unix "euc-jp", on MS-Windows cp932
2 korea Korean: on Unix "euc-kr", on MS-Windows cp949
2 prc simplified Chinese: on Unix "euc-cn", on MS-Windows cp936
2 chinese same as "prc"
2 taiwan traditional Chinese: on Unix "euc-tw", on MS-Windows cp950
u utf8 same as utf-8
u unicode same as ucs-2
u ucs2be same as ucs-2 (big endian)
u ucs-2be same as ucs-2 (big endian)
u ucs-4be same as ucs-4 (big endian)
default stands for the default value of 'encoding', depends on the
environment

posted @ 2008-08-14 10:15  h2appy  阅读(2034)  评论(0编辑  收藏  举报