How can I change a file's encoding with vim?

VimUnicode

Vim Problem Overview


I'm used to using vim to modify a file's line endings:

$ file file
file: ASCII text, with CRLF line terminators
$ vim file
:set ff=mac
:wq
$ file file
file: ASCII text, with CR line terminators

Is it possible to use a similar process to change a file's unicode encoding? I'm trying the following, which doesn't work:

$ file file.xml
file.xml: Unicode text, UTF-16, little-endian
$ vim file
:set encoding=utf-8
:wq
$ file file.xml
file.xml: Unicode text, UTF-16, little-endian

I saw someone say that he could "set fileencoding=utf-8, then update and write the file, and it works," but I seem to be missing something, or else he was confused. I don't know what he meant by "then update."

Vim Solutions


Solution 1 - Vim

From the doc:

> :write ++enc=utf-8 russian.txt

So you should be able to change the encoding as part of the write command.

Solution 2 - Vim

Notice that there is a difference between

http://vimhelp.appspot.com/options.txt.html#%27encoding%27">set encoding
and

http://vimhelp.appspot.com/options.txt.html#%27fileencoding%27">set fileencoding

In the first case, you'll change the output encoding that is shown in the terminal. In the second case, you'll change the output encoding of the file that is written.

Solution 3 - Vim

While using vim to do it is perfectly possible, why don't you simply use iconv? I mean - loading text editor just to do encoding conversion seems like using too big hammer for too small nail.

Just:

iconv -f utf-16 -t utf-8 file.xml > file.utf8.xml

And you're done.

Solution 4 - Vim

Just like your steps, setting fileencoding should work. However, I'd like to add one "set bomb" to help editor consider the file as UTF8.

$ vim file
:set bomb
:set fileencoding=utf-8
:wq

Solution 5 - Vim

It could be useful to change the encoding just on the command line before the file is read:

rem On MicroSoft Windows
vim --cmd "set encoding=utf-8" file.ext
# In *nix shell
vim --cmd 'set encoding=utf-8' file.ext

See starting, --cmd.

Solution 6 - Vim

auto GUIEnter * set encoding=utf-8 should help

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskiphoppyView Question on Stackoverflow
Solution 1 - VimBrian AgnewView Answer on Stackoverflow
Solution 2 - VimJohanView Answer on Stackoverflow
Solution 3 - Vimuser80168View Answer on Stackoverflow
Solution 4 - VimFrancisView Answer on Stackoverflow
Solution 5 - VimHans GinzelView Answer on Stackoverflow
Solution 6 - VimHopeHoleView Answer on Stackoverflow