Friday, July 4, 2014

Re: Encoding and Fileencoding of a latin1 file

On Thursday, July 3, 2014 10:20:45 AM UTC-5, rameo wrote:
> I've written this in my _vimrc file
>
> if has("multi_byte")
> if &termencoding == ""
> let &termencoding = &encoding
> endif
> set encoding=utf-8
> set fileencoding=utf-8
> set fileencodings=ucs-bom,utf-8,latin1
> endif
>
> When I open a latin1 file in my editor VIM indicates [CONVERTED] after the file name under the statusline.
>
> Fileencoding has been converted to Latin1. Correct. The file will be saved in Latin1.

I assume you verified this, using ":set fileencoding?", or you have some other method of viewing the fileencoding option? "fileencoding" is not only the way to control what encoding is used when writing; "fileencoding" also controls how Vim *reads* the file data. If Vim guessed the "fileencoding" wrongly, then you will either see a conversion error, or the wrong characters (or missing glyphs) shown in your file.

> But my problem is that the Encoding is still in UTF-8: I see many squares in the latin1 file.
>

"encoding" is Vim's internal representation of character strings. There is NO problem that the global encoding is still utf-8. This is a good thing. Furthermore, since every single character is Latin1 is also representable in utf-8, this CANNOT be the cause of the squares in your latin1 file.

> 1) Why doesn't Vim also let me read the file in Latin1 (changes the encoding to latin1)?
>

If "fileencoding" actually is set to "latin1" automatically by Vim after reading the file, then Vim *did* read the file in "latin1". I think more likely, Vim didn't actually use latin1 to read the file, or your font is missing some glyphs for characters in your file (unlikely if Vim actually used latin1).

> To temporary resolve this problem I set the encoding manually to Latin1
> :setlocal enc=latin1 | :e
>

This won't work for a couple reasons. First of all, "encoding" is a global option ONLY. It controls how Vim internally represents character data; nothing more. So not only does the "setlocal" not work, but also you've corrupted the data Vim already has stored internally, because Vim doesn't do any conversion of existing data when you set "encoding".

> But I noted that it changes the global encoding to Latin1 and now I see UTF-8 files in other tabs in the wrong Latin1 encoding.
>
> 2) How can I set encoding only to the local buffer?
>
> What did I wrong?

The correct way to force a single file to load using a given encoding, is, for example:

:e ++enc=latin1

This is confusing, because it actually sets the *fileencoding* option, not the encoding option.

Note that your current "fileencodings" option, will actually prefer to load a file in utf-8 over latin1. If a file is valid in both utf-8 and in latin1, it will be loaded in utf-8.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment