Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use vim to convert my file to utf8?

Tags:

vim

utf-8

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

like image 787
Eric Johnson Avatar asked Feb 16 '12 11:02

Eric Johnson


People also ask

Does Vim use UTF-8?

Vim does not actually default 'encoding' to UTF-8. It defaults to latin1, but will change based on the locale of your environment.


2 Answers

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.

: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:

:set bomb 

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

like image 85
Eric Johnson Avatar answered Oct 20 '22 16:10

Eric Johnson


:w ++enc=utf-8 % 

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

like image 41
Michael Krelin - hacker Avatar answered Oct 20 '22 15:10

Michael Krelin - hacker