How to setup vim properly for editing in utf-8

后端 未结 2 1927
长情又很酷
长情又很酷 2020-12-08 08:00

I\'ve run into problems a few times because vim\'s encoding was set to latin1 by default and I didn\'t notice and assumed it was using utf-8. Now that I have, I\'d like to s

2条回答
  •  佛祖请我去吃肉
    2020-12-08 08:55

    In response to sehe, I'll give a go at answering my own question! I removed the updates I made to the original question and have moved them to this answer. This is probably the better way to do it.

    The answer:

    if has("multi_byte")
      if &termencoding == ""
        let &termencoding = &encoding
      endif
      set encoding=utf-8                     " better default than latin1
      setglobal fileencoding=utf-8           " change default file encoding when writing new files
    endif
    

    I removed the bomb line because according to the BOM Wikipedia page it is not needed when using utf-8 and in fact defeats ASCII backwards compatibility. As long as ucs-bom is first in fileencodings, vim will be able to detect and handle existing files with BOMs, so it is not needed for that either.

    I removed the fileencodings line because it is not needed in this case. From the Vim docs: When 'encoding' is set to a Unicode encoding, and 'fileencodings' was not set yet, the default for 'fileencodings' is changed.

    I am using setglobal filencoding (as opposed to set fileencoding) because: When reading a file, fileencoding will be automatically set based on fileencodings. So it only matters for new files then. And according to the docs again:

    For a new file the global value of 'fileencoding' is used.

提交回复
热议问题