Linux系统攻略 Vim的编码及字体问题详解
2010-02-01 13:10
393 查看
发布时间:2007.07.30 06:16
来源:赛迪网
作者:sixth
和所有的流行文本编辑器一
样,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文件
附2:
(责任编辑:云子)
来源:赛迪网
作者:sixth
和所有的流行文本编辑器一
样,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 |
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 |
相关文章推荐
- Linux系统攻略 Vim的编码及字体问题详解
- Vim的编码及字体问题详解
- 今天在Mac机器上使用了Flex Builder编辑了一个源代码文件,保存后使用vim命令去打开时发现系统自动在每一行的结尾添加了^M符号,其实^M在Linux/Unix中是非常常见的,也就是我们在Win中见过的/r回车符号。由于编辑软件的编码问题,某些IDE的编辑器在编辑完文件之后会自动加上这个^M符号。看起来对我们的源代码没有任何影响,其实并不然,当我们把源代码文件Check In到svn之类
- Linux/Windows系统UTF-8编码问题
- linux下编码和vim编码问题解决
- 【转】convmv 工具使用----Linux/Windows系统UTF-8编码问题
- linux系统基本命令之管理输入输出,vim详解
- linux下由于系统编码问题造成乱码的解决办法
- 润乾报表在linux系统下字体显示问题解决方案
- MySQLdb错误 编码问题 linux系统粘贴板 python取环境变量 2016.05.27回顾
- iReport部署到Linux系统字体问题
- Linux下因为系统编码问题造成乱码的解决办法
- 解决JasperReport在Linux系统下找不到字体的问题
- Linux入门(7)——Ubuntu16.04安装wps并解决系统缺失字体问题
- linux下因为系统编码问题造成乱码的快速解决方法
- Linux系统网卡配置详解以及—常见问题的解决方法以及—硬盘移植后网卡的配置
- 详解Linux系统中inode索引节点已满问题的解决方法
- linux系统下vim命令详解
- linux下由于系统编码问题造成乱码的解决办法
- linux系统中的定时器crontab的使用攻略、常见错误问题和运行.jar文件的具体实例。