0

Is it possible?

I only know how it can be done for a named buffer (file). It should be reopened with

++enc=<encoding> option. But an unnamed buffer cannot be reopened.

I executed :set encoding=utf-8 but the text displayed in VIM was still unreadable. Only after I saved the buffer to a file, opened it and reopened it with :e ++enc=utf-8 it could be read well. However I noticed that there was an error [ILLEGAL BYTE in line 37]. But I'm happy with that. The main thing is that the contents are readable.

There was no error when I ran :set encoding=utf-8, but nothing seemed to happen too.

VIM 7.4

0

There's no need to re-open it - while in command mode, you can set the encoding to whatever you like with:

:set encoding=whatever

e.g.

:set encoding=utf-8

BTW, type :help encoding-names in vim for a list of supported encodings.

  • I've tried it, but there may be a difference between '++enc' and 'set encoding'. I'll update my question. – ka3ak Dec 25 '17 at 10:06
  • maybe put it in your ~/.vimrc so that utf-8 is the default encoding (i'm a little surprised it isn't aready the default...unless your .vimrc has decades worth of cruft like mine) – cas Dec 25 '17 at 10:21
  • It is the default encoding. Because if I start vim and execute ':set encoding?' it shows me 'encoding=utf-8'. I don't know whether this is an intentional behaviour or not but '++enc' seems to be able to force encoding regardless whether there are some invalid characters there while 'set encoding' can't. – ka3ak Dec 25 '17 at 10:27
0

The 'encoding' option is about Vim's internal representation of text in all buffers. This should not be changed after Vim has started, and it doesn't (at least not directly) affect the encoding of a buffer!

You're looking for the :help 'fileencoding' option:

:setlocal fenc=...

To summarize: What kind of characters can be typed and represented within a new buffer is controlled by the 'encoding' option. The "best" value for that is utf-8, as only that can represent any character (within Unicode). Now, if you've created an unnamed buffer and want to store it in a different coding, you use 'fileencoding' to change it (e.g. to ucs-2le) for the first and subsequent writes. (:w ++enc=... also works, but only for that particular write!)

In contrast, for the detection of opened files, you'd rather configure the 'fileencodings' option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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