When vim sources a file (vimrc
, or :so ...
), how does it know in what encoding the file was written? Can I specify the encoding with which it should source the file?
1 Answer
Vim has a built-in option for that:
'encoding'
It's by default "latin1" or the value of the environment variable $LANG
(e.g. in my case :echo $LANG
returns en_US.UTF-8
).
The value of encoding is derived from $LANG
, not taken verbatim. en_US.UTF-8
would give you utf-8
. (credits to @romainl)
You can control this value like any other option in vim:
set encoding=...
Please note that in the case of your vimrc
, you cannot take advantage of the modeline for setting this option. As said in the doc:
This option cannot be set from a modeline. It would most likely
corrupt the text.
You can also have a look at the different encoding options proposed in Vim:
:h scriptencoding
:h 'encoding'
:h 'fileencoding'
:h 'termencoding'
-
1Note that the value of
encoding
is derived from$LANG
, not taken verbatim.en_US.UTF-8
would give youutf-8
.– romainlJul 26, 2016 at 8:28 -
1Setting encoding is the wrong way, it will possibly invalidate all your registers and all variables holding strings. The correct way is to use the :scriptencoding command. Jul 26, 2016 at 9:06