10

I've upgraded vim and now it's unhappy about some UTF-8 values in my .vimrc.

This lead me to the question -- when vim reads .vimrc, what encoding does it assume the file has? I.e. what's fileencoding then?

2 Answers 2

7

It is whatever you define with :scriptencoding. It should be the first line in you .vimrc file, e.g.:

scriptencoding utf-8

set number
set ...

However, as per the Help for :scriptencoding, if you set the 'encoding' option in your vimrc, :scriptencoding must be placed after that.

Be aware that some options (listchars for example) only accept single width values.

Edit: Yeah, Vim is weird. encoding is for the editor itself, fileencoding is for the buffer and scriptencoding is for Vim scripts.

3
  • Thank you for the response, I was able to fix my installation with hacks. I'd appreciate if the answer was updated with an example of ~/,vimrc showing how to use scriptencoding
    – user20236
    Commented Jan 24, 2019 at 2:19
  • @DimaTisnek done! I din't want to write it in the answer because it is not what you asked for but Vim's defaults are messed up IMHO and I think that's a good reason to try out Neovim (in fact I had to look in my old configuration to remember the syntax of scriptencoding)
    – Tae
    Commented Jan 24, 2019 at 17:59
  • 1
    @Tae, unless you on windows, where Neovim is still way behind vim.
    – Maxim Kim
    Commented Jan 25, 2019 at 10:18
1

As hinted at in the help files (:help scriptencoding), if you do not use scriptencoding, vim does no conversion, effectively assuming the script is in the encoding of the 'encoding' option. This is very often utf-8, occasionally latin1. Other encodings are possible. Generally you want to use

scriptencoding utf-8

when your script file contains non-ascii characters and is encoding in utf-8. If you use only 7-bit ascii characters, it is unnecessary.

'fileencoding' applies to the current buffer, not scripts. When opening a file, vim tries to guess the encoding from the option 'fileencodings'.

1
  • Thank you for the response, I was able to fix my installation with hacks. I'd appreciate if the answer was updated with an example of ~/,vimrc showing how to use scriptencoding
    – user20236
    Commented Jan 24, 2019 at 2:19

Your Answer

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