Read more

Using Vim to repair files with incorrect character encoding/representation

Arne Hartherz
February 06, 2012Software engineer at makandra GmbH

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

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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.

Posted by Arne Hartherz to makandra dev (2012-02-06 14:43)