I have a text file. I've been told to make it UTF8. How can I do that with Vim?
Asked
Active
Viewed 7.8k times
145
-
possible duplicate of [Vim's encoding options](http://stackoverflow.com/questions/8121609/vims-encoding-options) – Benoit Feb 16 '12 at 12:25
-
possible duplicate of [How can I change a file's encoding with vim?](http://stackoverflow.com/questions/778069/how-can-i-change-a-files-encoding-with-vim) – Lætitia Apr 18 '14 at 11:20
2 Answers
223
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
.

Eric Johnson
- 17,502
- 10
- 52
- 59
-
2The instructions turn out to work fine, but: 1) utf-8 doesn't have endianness (like [vim help explains](http://vimdoc.sourceforge.net/htmldoc/mbyte.html#bom-bytes), the utf-8 BOM merely indicates that the file _is_ utf-8); 2) When you open the file again later, you need to have `set fileencoding=utf-8` again up front. Vim doesn't even notice the BOM if you told it to write one (and it does indeed write it). To make utf-8 work, you need that or `set encoding=utf-8` in your startup settings. At least, that's the story on my system. – Stein Apr 22 '19 at 14:09
-
Great answer, upvoted! If you want to know that it worked, check with `file -bi filename.txt` before and after, and you'll see `"text/plain; charset=utf-8"` after. – HoldOffHunger Nov 10 '21 at 17:07
-
40
:w ++enc=utf-8 %
to write the file in utf-8 encoding to disk.

Michael Krelin - hacker
- 138,757
- 24
- 193
- 173
-
8Although this is correct, your answer shouldn't have collected so many upvotes compared to Eric Johnson's. The reason is that if you don't set `fileencoding`, the `:w ++enc=utf-8` is valid one time, but next time you run `:w`, the value of `'fileencoding'` will be used, and if you haven't changed it (explicitly while editing, or by reloading the file, hoping that `'fencs'` is set appropriately and the actual encoding is well-detected), the old encoding will come back. – Benoit Feb 16 '12 at 12:23
-
@Benoit, I do not change `fileencoding`, because I am answering the question, not just telling everything I know about encodings and how vim works with them. It may take a while. – Michael Krelin - hacker Feb 16 '12 at 13:54
-
1@MichaelKrelin-hacker, changing `fileencoding` is also a valid answer to the question, which does not IMO lead to taking bad habits. But OK, that's just a matter of mood I suppose. – Benoit Feb 16 '12 at 13:56
-
3@Benoit, I didn't imply Eric's answer is not valid! If I find anything strange about his answer, it's not the content, but rather why did he post the question and the answer to his own question in rapid succession :) – Michael Krelin - hacker Feb 16 '12 at 13:59
-
-
3@Michael Krelin - hacker: check the FAQ. It is actually encouraged to answer your own question. It's the whole point of a Q&A page like this one. It doesn't matter who answers. – 0xC0000022L Feb 04 '13 at 23:08
-
2@0xC0000022L, last time I checked (haven't checked now), there was something about coming up with a solution after doing the research, not about posting question and answer in a *minute*. That said, I was only talking about what is *strange* about his answer, not what makes it invalid or something. – Michael Krelin - hacker Feb 04 '13 at 23:18