如何正确设置vim在utf-8中进行编辑 [英] How to setup vim properly for editing in utf-8
问题描述
我已经遇到了几次问题,因为vim的编码默认设置为latin1,我没有注意到,并假定它使用utf-8。现在我有,我想设置vim,以便在所有明显的情况下都会做正确的事情,默认情况下使用utf-8。
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 set up vim so that it will do the right thing in all obvious cases, and use utf-8 by default.
<强>我想避免的一些:
- 强制将某些其他编码保存的文件在我的更改之前工作打开为utf-8,导致乱码。
- 强制不支持多字节字符的终端(如Windows XP)要尝试
- 干扰其他程序读取或编辑文件的能力(我有一个(也许是不合理的)厌恶使用BOM默认情况下,因为我不清楚关于它可能会混乱其他程序的可能性。)
- 其他我不太了解的问题,可以猜到(但希望你这样做!)
- Forcing a file saved in some other encoding that would have worked before my changes to open as utf-8, resulting in gibberish.
- Forcing a terminal that doesn't support multibyte characters (like the Windows XP one) to try to display them anyway, resulting in gibberish.
- Interfering with other programs' ability to read or edit the files (I have a (perhaps unjustified) aversion to using a BOM by default because I am unclear on how likely it is to mess other programs up.)
- Other issues that I don't know enough about to guess at (but hopefully you do!)
到目前为止我已经有了
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
"setglobal bomb " use a BOM when writing new files
set fileencodings=ucs-bom,utf-8,latin1 " order to check for encodings when reading files
endif
这是从 vim维基。我从 setglobal fileencoding
将 bomb
移动到自己的语句中,否则它实际上不起作用。由于我对BOM的不确定性,我也评论了这一行。
This is taken and slightly modified from the vim wiki. I moved the bomb
from setglobal fileencoding
to its own statement because otherwise it doesn't actually work. I also commented out that line because of my uncertainty towards BOMs.
我在找什么:
- 可能的陷阱,以避免我错过
- 现有代码的问题
- 链接到已经讨论过/已经列出的任何地方
最终,我希望这样可以产生一个不需要的副本/粘贴片段,将为默认设置vim的vim设置,可以跨平台运行。
Ultimately, I'd like this to result in a no-thought-required copy/paste snippet that will set up vim for utf-8-by-default that will work across platforms.
编辑:我标记为自己的答案现在被接受,只要我能告诉它的工作正常,并记录所有可以合理地解释的事情。但它不是石头;如果您有任何新信息,请随时回答!
I've marked my own answer as accepted for now, as far as I can tell it works okay and accounts for all things it can reasonably account for. But it's not set in stone; if you have any new information please feel free to answer!
推荐答案
为了回应sehe,我会回答我自己的问题!我删除了我对原始问题的更新,并将其移到了这个答案。这可能是更好的方式。
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.
答案:
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
我删除了 bomb
行,因为根据 BOM维基百科页面使用utf-8时并不需要,实际上却是违反ASCII后向兼容性。只要 ucs-bom
首先在 fileencodings
中,vim将能够使用BOM检测和处理现有文件,所以它也不需要。
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.
我删除了 fileencodings
行,因为在这种情况下不需要。从 Vim docs :当编码'设置为Unicode编码,并且'fileencodings'尚未设置,'fileencodings'的默认值已更改。
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.
我是使用 setglobal filencoding
(而不是 set fileencoding
),因为:
读取文件时, fileencoding
将根据 fileencodings
自动设置。所以这只对新文件有用。另请参阅 docs :
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:
对于新文件,使用全局值
'fileencoding'。
For a new file the global value of 'fileencoding' is used.
这篇关于如何正确设置vim在utf-8中进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!