0

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?

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
René Nyffenegger
  • 2,105
  • 20
  • 36

1 Answers1

1

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'
nobe4
  • 16,033
  • 4
  • 48
  • 81
  • 1
    Note that the value of encoding is derived from $LANG, not taken verbatim. en_US.UTF-8 would give you utf-8. – romainl Jul 26 '16 at 08:28
  • 1
    Setting 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. – Christian Brabandt Jul 26 '16 at 09:06