解决linux下vim中文乱码的方法
<p><span><strong>Vim编码的详细介绍</strong></span></p>
<p>
Vim和所有的流行文本编辑器一样,Vim 可以很好的编辑各种字符编码的文件,这当然包括 UCS-2、UTF-8 等流行的 Unicode 编码方式。</p>
<p>
Vim 有四个跟字符编码方式有关的选项,encoding、fileencoding、fileencodings、termencoding (这些选项可能的取值请参考 Vim 在线帮助 :help encoding-names),它们的意义如下:</p>
<p>
1、encoding: Vim 内部使用的字符编码方式,包括 Vim 的 buffer (缓冲区)、菜单文本、消息文本等。用户手册上建议只在 .vimrc 中改变它的值,事实上似乎也只有在 .vimrc 中改变它的值才有意义。</p>
<p>
2、fileencoding: Vim 中当前编辑的文件的字符编码方式,Vim 保存文件时也会将文件保存为这种字符编码方式 (不管是否新文件都如此),网上是这样介绍的,但是我这样做在.vimrc中定义为utf-8似乎没有作用,只能在打开vim文件时手动设置才会起效,不知道什么原因。</p>
<p>
3、fileencodings: Vim 启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的字符编码方式,并且将 fileencoding 设置为最终探测到的字符编码方式。因此最好将 Unicode 编码方式放到这个列表的最前面,将拉丁语系编码方式 latin1 放到最后面。<br>
</p>
<p>
4、termencoding: Vim 所工作的终端 (或者 Windows 的 Console 窗口) 的字符编码方式。这个选项在 Windows 下对我们常用的 GUI 模式的 gVim 无效,而对 Console 模式的 Vim 而言就是 Windows 控制台的代码页,并且通常我们不需要改变它。</p>
<p>
<span><strong>再来记录一下 Vim 的多字符编码方式支持是如何工作的。</strong></span></p>
<p>
1、Vim 启动,根据 .vimrc 中设置的 encoding 的值来设置 buffer、菜单文本、消息文的字符编码方式。</p>
<p>
2、读取需要编辑的文件,根据 fileencodings 中列出的字符编码方式逐一探测该文件编码方式。并设置 fileencoding 为探测到看起来是正确的 字符编码方式,如果没有找到合适的编码,就用latin-1(ASCII)编码打开。</p>
<p>
3、对比 fileencoding 和 encoding 的值,若不同则调用 iconv 将文件内容转换为 encoding 所描述的字符编码方式,并且把转换后的内容放到为此文件开辟的 buffer 里,此时我们就可以开始编辑这个文件了。</p>
<p>
4、编辑完成后保存文件时,再次对比 fileencoding 和 encoding 的值。若不同,再次调用 iconv 将即将保存的 buffer 中的文本转换为 fileencoding 所描述的字符编码方式,并保存到指定的文件中。</p>
<p>
由于 Unicode 能够包含几乎所有的语言的字符,而且 Unicode 的 UTF-8 编码方式又是非常具有性价比的编码方式 (空间消耗比 UCS-2 小),因此建议 encoding 的值设置为 utf-8。这么做的另一个理由是 encoding 设置为 utf-8 时,Vim 自动探测文件的编码方式会更准确 (或许这个理由才是主要的 ;) 。我们在中文 Windows 里编辑的文件,为了兼顾与其他软件的兼容性,文件编码还是设置为 GB2312/GBK 比较合适,因此 fileencoding 建议设置为 chinese (chinese 是个别名,在 Unix 里表示 gb2312,在 Windows 里表示 cp936,也就是 GBK 的代码页)。</p>
<p>
<span><strong>解决linux下vim中文乱码的方法 </strong></span></p>
<p>
<strong>1、下载</strong></p>
<p>
可到http://www.vim.org/下载最新的7.3版本的vim源码。 </p>
<p>
<strong>2、安装</strong></p>
<p>
在编译之前,先要<code>./configure --help</code>查看一下配置选项, </p>
<p>
其中这个配置需要加进来<code>:--enable-multibyte Include multibyte editing support</code></p>
<p>
其作用是支持多字节编码,这一步应该是很重要。不然后面的配置貌似没什么用,不知道我说得对不对。</p>
<p>
在配置好了以后,自然是常规的:<code>make </code>、<code>make install</code>了</p>
<p>
<strong>3、最后是vimrc的脚本配置了</strong></p>
<p>
编辑<code>~/.vimrc</code>文件,加上如下几行:</p>
<div class="jb51code">
<div>
<div class="syntaxhighlighterbash" id="highlighter_261398">
<div class="toolbar">
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td class="gutter">
<div class="line number1 index0 alt2">
1</div>
<div class="line number2 index1 alt1">
2</div>
<div class="line number3 index2 alt2">
3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2">
<code class="bash functions">set</code> <code class="bash plain">fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936</code>
</div>
<div class="line number2 index1 alt1">
<code class="bash functions">set</code> <code class="bash plain">termencoding=utf-8</code>
</div>
<div class="line number3 index2 alt2">
<code class="bash functions">set</code> <code class="bash plain">encoding=utf-8</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div class="codetool" id="codetool">
<div class="code_n">
<textarea></textarea>
</div>
</div>
</div>
<p>
即可搞定。</p>
<p>
<span><strong>总结</strong></span></p>
<p>
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。</p>
<p>
原文链接:http://www.cnblogs.com/joeyupdo/archive/2013/03/03/2941737.html</p>
頁:
[1]