博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vim字符编码与显示
阅读量:4113 次
发布时间:2019-05-25

本文共 6458 字,大约阅读时间需要 21 分钟。

转自:http://blog.chinaunix.net/u1/37553/showart_347927.html

 

     和所有的流行文本编辑器一样,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

转载地址:http://enzpi.baihongyu.com/

你可能感兴趣的文章
关于Content-Length
查看>>
WebRequest post读取源码
查看>>
使用TcpClient可避免HttpWebRequest的常见错误
查看>>
EntityFramework 学习之一 —— 模型概述与环境搭建 .
查看>>
C# 发HTTP请求
查看>>
启动 LocalDB 和连接到 LocalDB
查看>>
Palindrome Number --回文整数
查看>>
Reverse Integer--反转整数
查看>>
Container With Most Water --装最多水的容器(重)
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
Pascal's Triangle -- 生成杨辉三角
查看>>