且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VIM集编码和文件编码utf-8

更新时间:2023-11-27 11:26:58

TL; DR




在第一种情况下,您将更改输出在
终端中显示为 的编码。在第二种情况下,您将更改 b
$ b




的输出编码

更多细节



wiki VIM关于使用unicode



encoding 设置如何 vim将代表字符内部。对于大多数口味的Unicode,Utf-8是必需的。



fileencoding 设置特定文件的编码(缓冲区本地); setglobal设置默认值,也可以使用一个空值:它默认与'encoding'相同。或者您可能想要设置其中一个ucs编码,它可能使相同的磁盘文件更大或更小,具体取决于特定的字符组合。另外,IIUC,utf-8总是大端(高位优先),而ucs可以是大端或小端,所以如果你使用它,你可能会nee d设置炸弹(见下文)。



代码示例

  if has(multi_byte)
如果& termencoding ==
let& termencoding =& encoding
endif
set encoding = utf-8
setglobal fileencoding = utf-8
setglobal bomb
set fileencodings = ucs-bom,utf-8,latin1
endif


What is the different between the two? :

  • set encoding=utf-8
  • set fileencoding=utf-8

Do I need to set both?
I want to use utf-8. Also, do I need to set fileencoding with set or setglobal?

TL;DR

In the first case, you'll change the output encoding that is shown in the terminal. In the second case, you'll change the output encoding of the file that is written.

More details

From the wiki of VIM about working with unicode

"encoding sets how vim shall represent characters internally. Utf-8 is necessary for most flavors of Unicode."

"fileencoding sets the encoding for a particular file (local to buffer); :setglobal sets the default value. An empty value can also be used: it defaults to same as 'encoding'. Or you may want to set one of the ucs encodings, It might make the same disk file bigger or smaller depending on your particular mix of characters. Also, IIUC, utf-8 is always big-endian (high bit first) while ucs can be big-endian or little-endian, so if you use it, you will probably need to set 'bomb" (see below)."

Example of code :

if has("multi_byte")
  if &termencoding == ""
    let &termencoding = &encoding
  endif
  set encoding=utf-8
  setglobal fileencoding=utf-8
  "setglobal bomb
  set fileencodings=ucs-bom,utf-8,latin1
endif