Modify the default character encoding for the Vim text editor

Image By default, Vim tries to detect the character encoding of a file when it is edited and it will use that encoding when saving the file. In case of UTF-8 files, Vim will search for a BOM at the beginning of the file and, if not found (relatively likely), it will use the encoding of the platform (as defined by the LANG variable on Unix, or by the locale setting on Windows), or Latin-1 if no platform encoding is set.

You can always detect what is the current encoding with the following command under Vim:

:set encoding

You can also set a specific encoding for the duration of the editing session with this command:

:set encoding=utf-8

The command above sets the character encoding when saving the file. It also sets it for the internal buffers of Vim, unless the variable fileencoding is specified.

This can have dramatic effects in case you are editing an UTF-8 file and the default internal format for the buffers of Vim is set to something else than UTF-8 via the fileencoding variable.

You can check the value of this second variable using the command:

:set fileencoding

Similarly, you can specify a value with:

:set fileencoding=utf-8

If you always want to reuse the same encoding, you can set the default value for these two variables in a .vimrc (Unix) or _vimrc (Windows) file, saved into your $HOME directory (%HOMEDRIVE%%HOMEPATH% on Windows).

Don’t forget to restart Vim for the changes to take effect.

This entry was posted in Tips and tricks and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s