256

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."

6 Answers 6

Help us improve our answers.

Are the answers below sorted in a way that puts the best answer at or near the top?

277

From the doc:

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

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

1
183

Notice that there is a difference between

set encoding

and

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.

1
  • 1
    thank you! Apache was outputting utf-8, so was php, so the browser said, so vim said with set encoding, and still the pages showed mangled characters that were alright as iso-8859-1. using set fileencoding showed a pretty 'Latin1' Mar 8, 2010 at 18:29
8

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.

1
  • 4
    The first variation should also work on *nix shells. 'single quotes' are only needed to escape all meta characters, which is usually not what you want.
    – jpaugh
    Feb 6, 2017 at 15:36
57

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
5
  • 8
    Thanks for your answer, it led me to learn more about the UTF byte order mark. However FYI, setting a BOM seems unnecessary/inadvisable for UTF-8 since it's not a fixed byte-length format like 16 or 32. See here for an explanation and reference. It's not a problem (and even helpful) for vim, I just thought people should just be aware that it may cause compatibility issues elsewhere.
    – joelhardi
    Jun 1, 2011 at 19:22
  • 2
    Is it bomb or bom, and can it be unset? EDIT: Yes, you can remove it via set nobomb.
    – icedwater
    Jul 1, 2014 at 2:33
  • 7
    Yes, VIm set up us the bomb (with a b).
    – ruffin
    Oct 16, 2014 at 14:57
  • 1
    per the docs, :set bomb is turned on if :set fenc=utf-8.. see :he bomb Dec 4, 2014 at 22:15
  • 13
    all our base encoding are now belong to UTF-8
    – roblogic
    Aug 25, 2015 at 1:49
84

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.

6
  • 19
    Downside, iconv might not be easily available on Windows. Mar 8, 2010 at 18:23
  • 4
    @AdrianoVaroliPiazza neither vim.
    – user1598585
    Mar 6, 2015 at 21:58
  • 1
    I'd say multiple "just install" downloads, with even a portable edition" constitutes "easily available". Mar 7, 2015 at 17:41
  • 2
    @adriano-varoli-piazza No, iconv is available on Windows with Cygwin and MingW, as @ coder-tim noted.
    – t0r0X
    May 18, 2017 at 23:08
  • 2
    @mario No, Vim very easily available on Windows: vim.org/download.php#pc
    – t0r0X
    May 18, 2017 at 23:08
0

auto GUIEnter * set encoding=utf-8 should help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.