Set encoding and fileencoding to utf-8 in Vim

VimEncoding

Vim Problem Overview


What is the difference between these two commands?

  • set encoding=utf-8
  • set fileencoding=utf-8

Do I need to set both when I want to use utf-8?

Also, do I need to set fileencoding with set or setglobal?

Vim Solutions


Solution 1 - Vim

TL;DR

> In the first case with set encoding=utf-8, you'll change the output encoding that is shown in the terminal. > > In the second case with set fileencoding=utf-8, you'll change the output encoding of the file that is written.

As stated by @Dennis, you can set them both in your ~/.vimrc if you always want to work in utf-8.

More details

From the wiki of VIM about working with unicode

"encoding sets how vim shall represent characters internally. Utf-8 is necessary for most flavors of Unicode."

"fileencoding sets the encoding for a particular file (local to buffer); :setglobal sets the default value. An empty value can also be used: it defaults to same as 'encoding'. Or you may want to set one of the ucs encodings, It might make the same disk file bigger or smaller depending on your particular mix of characters. Also, IIUC, utf-8 is always big-endian (high bit first) while ucs can be big-endian or little-endian, so if you use it, you will probably need to set 'bomb" (see below)."

Solution 2 - Vim

set encoding=utf-8  " The encoding displayed.
set fileencoding=utf-8  " The encoding written to file.

You may as well set both in your ~/.vimrc if you always want to work with utf-8.

Solution 3 - Vim

You can set the variable 'fileencodings' in your .vimrc.

> This is a list of character encodings considered when starting to edit an existing file. When a file is read, Vim tries to use the first mentioned character encoding. If an error is detected, the next one in the list is tried. When an encoding is found that works, 'fileencoding' is set to it. If all fail, 'fileencoding' is set to an empty string, which means the value of 'encoding' is used.

See :help filencodings

If you often work with e.g. cp1252, you can add it there:

set fileencodings=ucs-bom,utf-8,cp1252,default,latin9

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKiraly ZoltanView Question on Stackoverflow
Solution 1 - VimAdrien LacroixView Answer on Stackoverflow
Solution 2 - VimDennisView Answer on Stackoverflow
Solution 3 - VimMike11View Answer on Stackoverflow