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?

1 Answer 1

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'
2
  • 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, 2016 at 8: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. Jul 26, 2016 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.