r2 - 17 Sep 2008 - 06:26:07 - Main.yfangYou are here: YWiki >  MyTroubleShooting Web > Vim识别编码的问题

Vim识别编码的问题

问题描述

  • 时间 2009.9.17
  • 环境 CentOS5 + VIM - Vi IMproved 7.0
  • 症状 根据程序的编码应该生成utf8的汉字,而且系统的环境也是utf8的,用编码设成utf8的putty vim 这个文章看到的是乱码

问题处理过程

  • 本来以为是程序错了,换用两种perl的方法生成文件,居然看到的一直是乱码
  • 后来尝试把这个文件发送到另一台windows PC上,去掉目标字段以外所有内容,然后用浏览器打开,居然是正确的utf8页面,这就说明程序没有错,生成出来的文件就是utf8的
  • 为了确认上面的推测,直接cat 这个文件,看到的就是正确的输出,没有乱码
  • 问题锁定在vim上了,我也在这台机器上用vim看过不少utf8的文档,怎么这回就不行了呢?
    • 根据网上查到的资料,执行:set fenc,赫然看到结果是latin-1,怪不得不行,究其根源,我的文档中有一部分gbk的输出干扰了vim对文档编码的判断,最后无奈选用了latin-1

解决方案

  • 删除gbk的那段文字,再次打开,就是正常的了
  • 更好的做法是写 .vimrc,把初始的设置做好,这也能使后面的工作事倍功半,我这点做的不好,因为不是desktop,好多服务器,就没有设定过
  • 这里贴出网上搜到的相关文档,仅供参考
所有代码直接粘贴到终端运行即可!
安装程序
代码:
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

知识总结

  • Vim是根据文件的内容自动识别并判断编码的,想要知道当前的编码,执行
:set fenc
  • Vim中修改当前使用的编码的方式是
" 设定文件编码
set fileencoding=utf-8

" 你可以设定多个,系统会依次尝试
set fileencodings=utf-8,gb18030,utf-16,big5 

" 修改文件编码,以何种编码保存文件
:set fenc=utf-8
  • Perl 中转编码的两种方法,以下两种方法均实现将$pagecontent内容从gbk转成utf8存放到$converted的功能
# 方法一
use Text::Iconv;
my $converter = Text::Iconv->new("gbk", "utf8");
my $converted = $converter->convert("$pagecontent");

# 方法二
use Encode;
my $converted = Encode::encode("utf8", Encode::decode("gbk", $pagecontent));
  • Perl 中实现dos2unix的方法
# 这个太简单了,如果是文件就while <INPUT> 就行了,核心的匹配非常简单
$string =~ s/\r\n/\n/g;

参考资料

Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions
 
Powered by YWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding YWiki? Send feedback