Set encoding and fileencoding to utf-8 in Vim

Kiraly Zoltan picture Kiraly Zoltan · May 12, 2013 · Viewed 128.9k times · Source

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?

Answer

Adrien Lacroix picture Adrien Lacroix · May 12, 2013

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