LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1549|回复: 5

请问VIM配置文件的读取顺序到底是咋样的?(SOLVED)

[复制链接]
发表于 2008-8-1 16:15:04 | 显示全部楼层 |阅读模式
最近FVWM的ROOT菜单突然不能显示中文了,郁闷之下检查了字体设置,没有任何问题。百思不得其解,后来突然想到这个现象开始发生在我用VI修改了FVWM配置之后,于是怀疑起文件编码了,用file命令检查了一下这个配置文件,晕了,居然是ISO-8859 text。因为平时文本处理用的是Emacs,VIM只是用来临时修改一些配置文件,所以没怎么设置过VIM的环境,现在把配置文件转换编码后解决了。
想一劳永逸的解决文件编码方面的问题,查了一下知道负责vim文件保存编码的选项是“fileencoding”,我在~/.vimrc中加入了
set encoding=utf-8
set fileencoding=utf-8
然而发现一个奇怪的现象:

  1. clinif@localhost ~ $ vim test (内容只写英文)
  2. clinif@localhost ~ $ file test
  3. test: ASCII text
  4. clinif@localhost ~ $ vim test (添加一些汉字)
  5. clinif@localhost ~ $ file test
  6. test: ISO-8859 text
  7. clinif@localhost ~ $ rm test
  8. clinif@localhost ~ $ vim test2 (内容里包含一些汉字)
  9. clinif@localhost ~ $ file test2
  10. test2: UTF-8 Unicode text

  11. clinif@localhost ~ $ vim -u .vimrc -- test3 (内容里只写英文)
  12. clinif@localhost ~ $ file test3
  13. test: ASCII text
  14. clinif@localhost ~ $ vim -u .vimrc -- test3 (添加汉字)
  15. clinif@localhost ~ $ file test3
  16. test: UTF-8 Unicode text
复制代码

简单的描述就是:如果在一个纯英文的文本里面添加汉字,VIM不会按照fenc的设置将文件保存为UTF8,如果新建一个包含汉字的文本,那么fenc设置有效;
而启用-u选项(即强制指定VIM只使用~/.vimrc这一个配置文件)后,fenc设置才真正有效(即无论何时VIM总会把文件保存为UTF8格式)。
所以我怀疑系统中有什么地方的配置文件覆盖了~/.vimrc,或是与它有冲突?因为我VIM用的不多,没有自己设置过什么,基本上都是Gentoo安装好以后默认的设置,请熟悉VIM的朋友帮忙解释一下。

BTW:另一个小问题:用file查看一些文本文件的时候,发现给出的信息比较多,比如:
.fvwm/config: UTF-8 Unicode C++ program text(这个判断错了,这是我的FVWM配置,自然不是C++代码)
.vimrc: UTF-8 Unicode English text(这个文件里其实有中文注释)
test: UTF-8 Unicode text(这个怎么没有附加信息了?)
不知这些附加信息是怎么得来的?
发表于 2008-8-1 16:49:34 | 显示全部楼层
赞楼主的细心
回复 支持 反对

使用道具 举报

发表于 2008-8-1 19:07:06 | 显示全部楼层
没有观察到这种现象。我的VIM总是保存成UTF-8没有问题……

我觉得更可能的是你第一组测试里汉字加得太少,被file误判了。ISO-8859根本不支持编码汉字……

Post by clinif;1881239
最近FVWM的ROOT菜单突然不能显示中文了,郁闷之下检查了字体设置,没有任何问题。百思不得其解,后来突然想到这个现象开始发生在我用VI修改了FVWM配置之后,于是怀疑起文件编码了,用file命令检查了一下这个配置文件,晕了,居然是ISO-8859 text。因为平时文本处理用的是Emacs,VIM只是用来临时修改一些配置文件,所以没怎么设置过VIM的环境,现在把配置文件转换编码后解决了。
想一劳永逸的解决文件编码方面的问题,查了一下知道负责vim文件保存编码的选项是“fileencoding”,我在~/.vimrc中加入了
set encoding=utf-8
set fileencoding=utf-8
然而发现一个奇怪的现象:

  1. clinif@localhost ~ $ vim test (内容只写英文)
  2. clinif@localhost ~ $ file test
  3. test: ASCII text
  4. clinif@localhost ~ $ vim test (添加一些汉字)
  5. clinif@localhost ~ $ file test
  6. test: ISO-8859 text
  7. clinif@localhost ~ $ rm test
  8. clinif@localhost ~ $ vim test2 (内容里包含一些汉字)
  9. clinif@localhost ~ $ file test2
  10. test2: UTF-8 Unicode text

  11. clinif@localhost ~ $ vim -u .vimrc -- test3 (内容里只写英文)
  12. clinif@localhost ~ $ file test3
  13. test: ASCII text
  14. clinif@localhost ~ $ vim -u .vimrc -- test3 (添加汉字)
  15. clinif@localhost ~ $ file test3
  16. test: UTF-8 Unicode text
复制代码

简单的描述就是:如果在一个纯英文的文本里面添加汉字,VIM不会按照fenc的设置将文件保存为UTF8,如果新建一个包含汉字的文本,那么fenc设置有效;
而启用-u选项(即强制指定VIM只使用~/.vimrc这一个配置文件)后,fenc设置才真正有效(即无论何时VIM总会把文件保存为UTF8格式)。
所以我怀疑系统中有什么地方的配置文件覆盖了~/.vimrc,或是与它有冲突?因为我VIM用的不多,没有自己设置过什么,基本上都是Gentoo安装好以后默认的设置,请熟悉VIM的朋友帮忙解释一下。

BTW:另一个小问题:用file查看一些文本文件的时候,发现给出的信息比较多,比如:
.fvwm/config: UTF-8 Unicode C++ program text(这个判断错了,这是我的FVWM配置,自然不是C++代码)
.vimrc: UTF-8 Unicode English text(这个文件里其实有中文注释)
test: UTF-8 Unicode text(这个怎么没有附加信息了?)
不知这些附加信息是怎么得来的?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-8-1 21:07:44 | 显示全部楼层
Post by richardpku;1881313
没有观察到这种现象。我的VIM总是保存成UTF-8没有问题……

我觉得更可能的是你第一组测试里汉字加得太少,被file误判了。ISO-8859根本不支持编码汉字……


我刚才查了一下维基百科里关于ISO-8859的资料,ISO-8859是在ASCII的基础上定义的一个扩展字符集,加入了西方其它一些语言的字母符号。所以应该是VI可能用了某种双字节编码保存文件,但是没有被file等程序正确识别,所以把双字节当作单字节拆分开来显示了。
于是我做了下面这个测试(觉得长的话也可以跳过我测试的过程直接看我得出的结论,其实结论很简单,因为做这个测试之前没想到,所以试探过程麻烦了点):

  1. clinif@localhost ~ $ vi test (内容是abcdefg)
  2. clinif@localhost ~ $ file test
  3. test: ASCII text
  4. clinif@localhost ~ $ hexdump test (0x0a是换行符)
  5. 0000000 6261 6463 6665 0a67                    
  6. 0000008
  7. clinif@localhost ~ $ vi test (在原内容后添加"只是一个测试")
  8. clinif@localhost ~ $ file test
  9. test: ISO-8859 text
  10. clinif@localhost ~ $ hexdump test
  11. 0000000 6261 6463 6665 d667 cabb d2c7 b8bb b2f6
  12. 0000010 cae2 0ad4                              
  13. 0000014
  14. clinif@localhost ~ $ cat test (根据ISO-8859-1的编码表查询知道,后面的“乱码”部分基本符合上面多出的十六进制值)
  15. abcdefgֻÊÇһ¸ö²âÊÔ
  16. clinif@localhost ~ $ vi test2 (内容是“abcdefg只是一个测试”)
  17. clinif@localhost ~ $ file test2
  18. test2: UTF-8 Unicode text
  19. clinif@localhost ~ $ hexdump test2 (这才是UTF-8的编码内容)
  20. 0000000 6261 6463 6665 e567 aa8f 98e6 e4af 80b8
  21. 0000010 b8e4 e6aa 8bb5 afe8 0a95               
  22. 000001a
  23. clinif@localhost ~ $ cat test2
  24. abcdefg只是一个测试
  25. clinif@localhost ~ $
复制代码

当用VI打开那个所谓的ISO-8859的test文件的时候,输入命令“:set fenc=gb2312”没有任何变化,而如果输入“:set fenc=utf-8”,则提示文件已经改变。可见VIM是以GB2312编码保存的,为了确认这一猜想,我用vi新建了test3,内容与test文件相同,但是在保存的时候就使用了“:set fenc=gb2312”,结果hexdump出来的结果与test完全相同。可见我的VI确实是默认以GB2312保存文件的,基本上证明了我的猜测,有地方的配置文件覆盖了~/.vimrc里的设置。
附上我找到的/etc/vim/vimrc文件的内容(别处是否还有VI的配置我暂时不清楚),请熟悉VI配置文件的朋友帮忙看看,是不是GENTOO自带的这个配置覆盖了我的fenc设定?还是VIM是先读取了~/.vimrc然后又读取/etc下的配置?
呵呵,不好意思,这个文件默认就是这么长,我没改过,麻烦大家了!
  1. scriptencoding utf-8
  2. " ^^ Please leave the above line at the start of the file.

  3. " Default configuration file for Vim
  4. " $Header: /var/cvsroot/gentoo-x86/app-editors/vim-core/files/vimrc-r3,v 1.1 2006/03/25 20:26:27 genstef Exp $

  5. " Written by Aron Griffis <agriffis@gentoo.org>
  6. " Modified by Ryan Phillips <rphillips@gentoo.org>
  7. " Modified some more by Ciaran McCreesh <ciaranm@gentoo.org>
  8. " Added Redhat's vimrc info by Seemant Kulleen <seemant@gentoo.org>

  9. " You can override any of these settings on a global basis via the
  10. " "/etc/vim/vimrc.local" file, and on a per-user basis via "~/.vimrc". You may
  11. " need to create these.

  12. " {{{ General settings
  13. " The following are some sensible defaults for Vim for most users.
  14. " We attempt to change as little as possible from Vim's defaults,
  15. " deviating only where it makes sense
  16. set nocompatible        " Use Vim defaults (much better!)
  17. set bs=2                " Allow backspacing over everything in insert mode
  18. set ai                  " Always set auto-indenting on
  19. set history=50          " keep 50 lines of command history
  20. set ruler               " Show the cursor position all the time

  21. set viminfo='20,"500   " Keep a .viminfo file.

  22. " Don't use Ex mode, use Q for formatting
  23. map Q gq

  24. " When doing tab completion, give the following files lower priority. You may
  25. " wish to set 'wildignore' to completely ignore files, and 'wildmenu' to enable
  26. " enhanced tab completion. These can be done in the user vimrc file.
  27. set suffixes+=.info,.aux,.log,.dvi,.bbl,.out,.o,.lo

  28. " When displaying line numbers, don't use an annoyingly wide number column. This
  29. " doesn't enable line numbers -- :set number will do that. The value given is a
  30. " minimum width to use for the number column, not a fixed size.
  31. if v:version >= 700
  32.   set numberwidth=3
  33. endif
  34. " }}}

  35. " {{{ Modeline settings
  36. " We don't allow modelines by default. See bug #14088 and bug #73715.
  37. " If you're not concerned about these, you can enable them on a per-user
  38. " basis by adding "set modeline" to your ~/.vimrc file.
  39. set nomodeline
  40. " }}}

  41. " {{{ Locale settings
  42. " Try to come up with some nice sane GUI fonts. Also try to set a sensible
  43. " value for fileencodings based upon locale. These can all be overridden in
  44. " the user vimrc file.
  45. if v:lang =~? "^ko"
  46.   set fileencodings=euc-kr
  47.   set guifontset=-*-*-medium-r-normal--16-*-*-*-*-*-*-*
  48. elseif v:lang =~? "^ja_JP"
  49.   set fileencodings=euc-jp
  50.   set guifontset=-misc-fixed-medium-r-normal--14-*-*-*-*-*-*-*
  51. elseif v:lang =~? "^zh_TW"
  52.   set fileencodings=big5
  53.   set guifontset=-sony-fixed-medium-r-normal--16-150-75-75-c-80-iso8859-1,-taipei-fixed-medium-r-normal--16-150-75-75-c-160-big5-0
  54. elseif v:lang =~? "^zh_CN"
  55.   set fileencodings=gb2312
  56.   set guifontset=*-r-*
  57. endif

  58. " If we have a BOM, always honour that rather than trying to guess.
  59. if &fileencodings !~? "ucs-bom"
  60.   set fileencodings^=ucs-bom
  61. endif

  62. " Always check for UTF-8 when trying to determine encodings.
  63. if &fileencodings !~? "utf-8"
  64.   set fileencodings+=utf-8
  65. endif

  66. " Make sure we have a sane fallback for encoding detection
  67. set fileencodings+=default
  68. " }}}

  69. " {{{ Syntax highlighting settings
  70. " Switch syntax highlighting on, when the terminal has colors
  71. " Also switch on highlighting the last used search pattern.
  72. if &t_Co > 2 || has("gui_running")
  73.   syntax on
  74.   set hlsearch
  75. endif
  76. " }}}

  77. " {{{ Terminal fixes
  78. if &term ==? "xterm"
  79.   set t_Sb=^[4%dm
  80.   set t_Sf=^[3%dm
  81.   set ttymouse=xterm2
  82. endif

  83. if &term ==? "gnome" && has("eval")
  84.   " Set useful keys that vim doesn't discover via termcap but are in the
  85.   " builtin xterm termcap. See bug #122562. We use exec to avoid having to
  86.   " include raw escapes in the file.
  87.   exec "set <C-Left>=\eO5D"
  88.   exec "set <C-Right>=\eO5C"
  89. endif
  90. " }}}

  91. " {{{ Filetype plugin settings
  92. " Enable plugin-provided filetype settings, but only if the ftplugin
  93. " directory exists (which it won't on livecds, for example).
  94. if isdirectory(expand("$VIMRUNTIME/ftplugin"))
  95.   filetype plugin on

  96.   " Uncomment the next line (or copy to your ~/.vimrc) for plugin-provided
  97.   " indent settings. Some people don't like these, so we won't turn them on by
  98.   " default.
  99.   " filetype indent on
  100. endif
  101. " }}}

  102. " {{{ Fix &shell, see bug #101665.
  103. if "" == &shell
  104.   if executable("/bin/bash")
  105.     set shell=/bin/bash
  106.   elseif executable("/bin/sh")
  107.     set shell=/bin/sh
  108.   endif
  109. endif
  110. "}}}

  111. " {{{ Our default /bin/sh is bash, not ksh, so syntax highlighting for .sh
  112. " files should default to bash. See :help sh-syntax and bug #101819.
  113. if has("eval")
  114.   let is_bash=1
  115. endif
  116. " }}}

  117. " {{{ Autocommands
  118. if has("autocmd")

  119. augroup gentoo
  120.   au!

  121.   " Gentoo-specific settings for ebuilds.  These are the federally-mandated
  122.   " required tab settings.  See the following for more information:
  123.   " http://www.gentoo.org/proj/en/devrel/handbook/handbook.xml
  124.   " Note that the rules below are very minimal and don't cover everything.
  125.   " Better to emerge app-vim/gentoo-syntax, which provides full syntax,
  126.   " filetype and indent settings for all things Gentoo.
  127.   au BufRead,BufNewFile *.e{build,class} let is_bash=1|setfiletype sh
  128.   au BufRead,BufNewFile *.e{build,class} set ts=4 sw=4 noexpandtab

  129.   " In text files, limit the width of text to 78 characters, but be careful
  130.   " that we don't override the user's setting.
  131.   autocmd BufNewFile,BufRead *.txt
  132.         \ if &tw == 0 && ! exists("g:leave_my_textwidth_alone") |
  133.         \     setlocal textwidth=78 |
  134.         \ endif

  135.   " When editing a file, always jump to the last cursor position
  136.   autocmd BufReadPost *
  137.         \ if ! exists("g:leave_my_cursor_position_alone") |
  138.         \     if line("'"") > 0 && line ("'"") <= line("$") |
  139.         \         exe "normal g'"" |
  140.         \     endif |
  141.         \ endif

  142.   " When editing a crontab file, set backupcopy to yes rather than auto. See
  143.   " :help crontab and bug #53437.
  144.   autocmd FileType crontab set backupcopy=yes

  145. augroup END

  146. endif " has("autocmd")
  147. " }}}

  148. " {{{ vimrc.local
  149. if filereadable("/etc/vim/vimrc.local")
  150.   source /etc/vim/vimrc.local
  151. endif
  152. " }}}

  153. " vim: set fenc=utf-8 tw=80 sw=2 sts=2 et foldmethod=marker :
复制代码
回复 支持 反对

使用道具 举报

发表于 2008-8-2 12:10:08 | 显示全部楼层
vim保存文件时使用 fileencoding (fenc) 指定的编码
而读文件时根据 fileencodings (fencs) 所列的编码一个个地试,并把当前 fenc 改成试出来的那个编码

粗略看了一下你的vimrc文件,fencs貌似是 gb2312,utf-8,GB排在前面的,所以读纯 ASCII 文件的时候就被视为 gb2312 了。要是把 utf-8 排在 gb2312 前面,纯 ASCII 文件就会被当成 UTF8

你在 ~/.vimrc 里加一句 set fencs=utf-8,gb2312 应该就没问题了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-8-2 12:50:34 | 显示全部楼层
Post by richardpku;1881553
vim保存文件时使用 fileencoding (fenc) 指定的编码
而读文件时根据 fileencodings (fencs) 所列的编码一个个地试,并把当前 fenc 改成试出来的那个编码

粗略看了一下你的vimrc文件,fencs貌似是 gb2312,utf-8,GB排在前面的,所以读纯 ASCII 文件的时候就被视为 gb2312 了。要是把 utf-8 排在 gb2312 前面,纯 ASCII 文件就会被当成 UTF8

你在 ~/.vimrc 里加一句 set fencs=utf-8,gb2312 应该就没问题了


非常感谢!用了你的方法,问题解决了!
之前自己查资料时看到过fencs的说明,但是看到GENTOO里VIM的默认设置有很多关于fencs的猜测语句,心想应该会比我自己设定的要全面些,所以就没自己重新设定,倒是没想到fencs的判断还有顺序的问题,呵呵
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表