Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gVim incorrectly guesses file encoding

Tags:

vim

encoding

I'm running gVim 8.2 with default configuration on Windows 7 with russian language (so all the system text and menu items are in russian). When I open a utf8 file with russian text in it, it's displayed incorrectly in cp1251 for some reason:

:set encoding?
encoding=cp1251

manually setting :set encoding=utf8 fixes it.

Other encoding-related options have following values:

:set fileencoding?
fileencoding=
:set fileencodings?
fileencodings=ucs-bom

I find vim help confusing here, because it doesn't seem to explain how it guesses the encoding. For some reason other applications I tried (Notepad++, Sublime Text 4, even Windows Notepad) guess the file encoding correctly. As I mentioned in the beginning, I run gVim with default configuration, so there's no custom vimrc anywhere:

:echo $MYVIMRC
D:\Program Files (x86)\Vim\_vimrc

What would be the correct way to fix this problem?

like image 744
xaxa Avatar asked Oct 14 '25 07:10

xaxa


1 Answers

Create a vimrc with set encoding=utf-8 in it. This should be the default in newer versions of Vim on Windows, as can be seen from :help 'encoding'.

'encoding' 'enc'    string (default for MS-Windows: "utf-8",
                    otherwise: value from $LANG or "latin1")

The default value used to be latin1 on Windows but it was changed to utf-8 recently.

This should be enough to solve your issue.

Again from :help 'encoding':

    Sets the character encoding used inside Vim.  It applies to text in
    the buffers, registers, Strings in expressions, text stored in the
    viminfo file, etc.  It sets the kind of characters which Vim can work
    with.

Vim uses fileencodings (plural) to try and guess the encoding of your file. fileencoding (singular) is the encoding that Vim guessed (or that you've set) for your file. You probably don't need to change either of these.

like image 100
gdupras Avatar answered Oct 17 '25 17:10

gdupras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!