如何在 VIM 中设置文件的字符编码 [英] How to set the character encoding for a file in VIM
问题描述
我正在使用 :set fileencoding=utf-8
并且文件已正确保存,但是下次我打开它时,出现乱码和 :set fileencoding?
代码> 表示该选项未设置.
I'm using :set fileencoding=utf-8
and the file is saved correctly, but the next time I open it, I get garbled characters and :set fileencoding?
says the option isn't set.
从文档中我了解到我不应该触及 encoding
选项.
From the docs I understand that I shouldn't touch the encoding
option.
在 VIM 中为文件指定编码的正确方法是什么?
What's the correct way of specifying an encoding for a file in VIM?
推荐答案
使用
set fileencodings=utf-8
(以 s 结尾)可以包含不同编码的列表.Vim 将尝试列出的编码,从左到右,直到成功为止,它会将 fileencoding 设置为该编码.如果没有工作,则 fileencoding 设置为空字符串,这将导致默认行为.
(with an s at the end) which can contain a list of different encodings. Vim will try the encodings listed, from left to right, until one works and it will set fileencoding to that encoding. If none work then fileencoding is set to an empty string which will result in default behavior.
此外,将它添加到您的 vimrc 中可能是有意义的,这样您就不会经常这样做.
Also it would probably make sense to add that to your vimrc so you aren't constantly doing that.
这篇关于如何在 VIM 中设置文件的字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!