Using Vim to repair files with incorrect character encoding/representation

Updated . Posted . Visible to the public.

Consider you have a file that uses improper encoding on special characters. Example: You see the latin1 version "ñ" for the UTF-8 "ñ" but the file itself is stored as UTF-8 (meaning that the UTF-8 bytes are doubly encoded).

You can fix that easily with Vim:

vim broken.file

Now you tell vim that the file's encoding is actually latin1 (you can see what Vim is currently using by saying only :set fileencoding):

:set fileencoding=latin1

Write and reload the file:

:w
:e

All should be good now.

Adjust to your needs for any other encodings.

Arne Hartherz
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2012-02-06 13:43)