Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

unicode

People also ask

Does Vim load entire file into memory?

Yes, Vim loads the whole file into memory.

What file encoding does Linux use?

The most commonly used encodings are UTF-8, UTF-16 and the now-obsolete UCS-2. UTF-8 is a character encoding capable of encoding all possible characters, or code points,. Defined by Unicode and originally designed by Ken Thompson and Rob Pike. The encoding has a variable length and uses 8-bit code units.


From the doc:

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

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


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.


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.


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

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.