如何使用vim将文件转换为utf8?

时间:2021-10-09 19:19:21

I have a text file. I've been told to make it UTF8. How can I do that with Vim?

我有一个文本文件。我被告知要做UTF8。用Vim怎么做呢?

2 个解决方案

#1


121  

If you are editing a file encoded as latin1, you will find that 'fileencoding' for that buffer is set to latin1. So you will need to manually set the fileencoding before saving the file.

如果您正在编辑一个以latin1编码的文件,您将发现该缓冲区的“文件编码”被设置为latin1。因此,在保存文件之前需要手动设置文件编码。

:set fileencoding=utf8
:w myfilename

Also note that UTF8 files often begin with a Byte Order Mark (BOM) which indicates endianness. The BOM is optional but some programs use it exclusively to determine the file encoding. Under certain conditions Vim will write the BOM but sometimes it won't. To explicitly set the BOM do this:

还要注意,UTF8文件通常以字节顺序标记(BOM)开头,它指示了endianness。BOM是可选的,但是有些程序只使用它来确定文件编码。在某些情况下,Vim会写BOM,但有时不会。要显式地设置BOM,请执行以下操作:

:set bomb

For more information :help mbyte-options and :help utf8 and :help bomb.

更多信息:帮助mbyte选项和:帮助utf8和:帮助炸弹。

#2


28  

:w ++enc=utf-8 %

to write the file in utf-8 encoding to disk.

以utf-8编码将文件写入磁盘。

#1


121  

If you are editing a file encoded as latin1, you will find that 'fileencoding' for that buffer is set to latin1. So you will need to manually set the fileencoding before saving the file.

如果您正在编辑一个以latin1编码的文件,您将发现该缓冲区的“文件编码”被设置为latin1。因此,在保存文件之前需要手动设置文件编码。

:set fileencoding=utf8
:w myfilename

Also note that UTF8 files often begin with a Byte Order Mark (BOM) which indicates endianness. The BOM is optional but some programs use it exclusively to determine the file encoding. Under certain conditions Vim will write the BOM but sometimes it won't. To explicitly set the BOM do this:

还要注意,UTF8文件通常以字节顺序标记(BOM)开头,它指示了endianness。BOM是可选的,但是有些程序只使用它来确定文件编码。在某些情况下,Vim会写BOM,但有时不会。要显式地设置BOM,请执行以下操作:

:set bomb

For more information :help mbyte-options and :help utf8 and :help bomb.

更多信息:帮助mbyte选项和:帮助utf8和:帮助炸弹。

#2


28  

:w ++enc=utf-8 %

to write the file in utf-8 encoding to disk.

以utf-8编码将文件写入磁盘。