vimtricks.wiki Concise Vim tricks, one at a time.

How do I reopen the current file with a different character encoding in Vim?

Answer

:e ++enc=utf-8

Explanation

When Vim opens a file with the wrong encoding — producing garbled text or incorrect characters — you can reload it with a specific encoding using :e ++enc={encoding}. This overrides the encoding for the current buffer without changing any global settings.

How it works

  • :e reloads the current buffer from disk, discarding unsaved changes
  • ++enc= is a command modifier that sets the character encoding used to read the file
  • Common encoding values: utf-8, latin1, utf-16, utf-16le, cp1252, sjis
  • This is a per-command flag, so fileencoding and encoding options remain unchanged

Example

A file saved in Latin-1 but misdetected as UTF-8 shows garbled characters. Reopen it correctly:

:e ++enc=latin1

To open a Windows UTF-16 file:

:e ++enc=utf-16le

After reopening correctly, convert and save as UTF-8:

:set fileencoding=utf-8
:w

Tips

  • Use ++ff=dos or ++ff=unix similarly to force the line-ending format when reopening
  • :set fileencoding? shows what encoding Vim is currently using for the buffer
  • To open a different file with a specific encoding, use :e ++enc=latin1 otherfile.txt
  • If Vim consistently misdetects a file's encoding, add it to fileencodings: :set fileencodings+=latin1

Next

How do I duplicate every line matching a pattern, placing a copy directly below each one?