Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set encoding and fileencoding to utf-8 in Vim

Tags:

vim

encoding

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?

like image 943
Kiraly Zoltan Avatar asked May 12 '13 13:05

Kiraly Zoltan


People also ask

How do I change the encoding to UTF-8?

Click Tools, then select Web options. Go to the Encoding tab. In the dropdown for Save this document as: choose Unicode (UTF-8). Click Ok.

Is UTF-8 character set or encoding?

UTF-8 is a character encoding system. It lets you represent characters as ASCII text, while still allowing for international characters, such as Chinese characters. As of the mid 2020s, UTF-8 is one of the most popular encoding systems.


2 Answers

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

like image 140
Adrien Lacroix Avatar answered Oct 14 '22 09:10

Adrien Lacroix


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.

like image 32
Dennis Avatar answered Oct 14 '22 07:10

Dennis