[vim] Set encoding and fileencoding to utf-8 in Vim

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?

This question is related to vim encoding

The answer is


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

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.