Vim编码:Latin1和UTF-8

4

以下是我的问题:

  • when I open the file, I get this message from vim:

    "myfilename.sql" [converted] 78565L, 10487381C
    
  • if I do :set i get:

    Options
      backspace=2         colorcolumn=+1      formatoptions=qc    scrolloff=15        smartindent         textwidth=80        visualbell
      backup              expandtab           ignorecase          shiftwidth=4        syntax=sql          ttyfast             t_vb=
      bomb                filetype=sql        number              showcmd             tabstop=4           ttymouse=sgr
      backupdir=~/.vim/backup
      comments=s1:/*,mb:*,ex:*/,:--,://
      define=\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>
      directory=~/.vim/tmp
      fileencoding=utf-8
      fileencodings=utf-8,ucs-bom,default,latin1
      matchpairs=(:),{:},[:],<:>
      omnifunc=sqlcomplete#Complete
      viminfo='10,"100,:20,%,n~/.viminfo
    
  • I have the characters visible like this:

    vim bad encoding

  • I found that if I reload & force encoding latin1 with :e ++enc=latin1 I get right accents, but not all:

    vim almost good encoding

  • So I found out (the only solution) to get all the right visible encoding is to manually force back to utf8: :set encoding=utf8 then I get:

    vim good encoding

  • if I do :set i get:

    --- Options ---
      backspace=2         encoding=utf-8      formatoptions=qc    scrolloff=15        smartindent         textwidth=80        visualbell
      backup              expandtab           ignorecase          shiftwidth=4        syntax=sql          ttyfast             t_vb=
      colorcolumn=+1      filetype=sql        number              showcmd             tabstop=4           ttymouse=sgr
      backupdir=~/.vim/backup
      comments=s1:/*,mb:*,ex:*/,:--,://
      define=\c\<\(VARIABLE\|DECLARE\|IN\|OUT\|INOUT\)\>
      directory=~/.vim/tmp
      fileencoding=latin1
      fileencodings=utf-8,ucs-bom,default,latin1
      matchpairs=(:),{:},[:],<:>
      omnifunc=sqlcomplete#Complete
      viminfo='10,"100,:20,%,n~/.viminfo
    
  • As you can see, I see fileencoding=latin1 so I try to force it to utf8: :set fileencoding=utf-8. I save it :wq and whenever I want to re-open it nothing has changed, I still have to do all of this to get a proprer display with good accents!

  • The only thing I want from now is to save it so that I can re-open it without having all of this to do to get it right. What should I do?


当您首先使用 :set encoding=utf8 然后使用 :e ++enc=latin1 filename 会发生什么? - Ingo Karkat
@Зелёный 源代码 Pro ExtraLight 12 点。为什么? - Olivier Pons
@IngoKarkat 当我执行 :set encoding=utf8 时,它会更改显示但不是正确的(就像使用 :set encoding=latin1 一样),如果我尝试 :e ++enc=utf-8 otherfile.sql ,它会创建一个新的空文件... - Olivier Pons
@OlivierPons 我怀疑你通过 :w 搞乱了文件编码。你能否在终端中展示 file yourFile 或者 file -i yourFile 的输出? - Kent
@OlivierPons 打开vim,set encoding?的输出是什么? - Kent
显示剩余8条评论
2个回答

4

encoding和fileencoding是vim中的两个选项。详细信息请参阅帮助文档。

如果您的文件编码为UTF-8,则最简单的读取方法是设置encoding=utf-8fileencoding=utf-8

您可以在vimrc中添加这些行,使其成为默认的编码选项。


3

简短回答:我遇到了国际字符显示为▒的类似问题,解决方法是:

:set termencoding=utf-8

较长的答案:这里有三个相关设置。通常它们会自动做正确的事情,但如果它们没有,你可以设置它们。
:set encoding - 设置用于读取文件的编码
:set fileencoding - 设置保存文件时要使用的编码
:set termencoding - 设置用于向终端显示字符的编码
如果文件是UTF-8,但Vim将其读取为Latin1,同时您的终端是UTF-8,但Vim将其写入终端作为Latin1,则可能会出现您描述的奇怪结果。在这种情况下,尽管底层一切都错了,但字符可能看起来是正确的。您可以通过将光标放在问题字符上并按下ga来查找其编码值来检查此假设。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接