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.
But my problem is that the Encoding is still in UTF-8: I see many squares in the latin1 file.
1) Why doesn't Vim also let me read the file in Latin1 (changes the encoding to latin1)?
To temporary resolve this problem I set the encoding manually to Latin1
:setlocal enc=latin1 | :e
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?
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.
By the way, I'm more of a purist and want my Latin1 files to actually be Latin1,
using cp1252 only occasionally when I know it will work.
For that reason, my Vim config contains this encoding logic (actually this is
simplified from my full config) that will detect files as cp1252 normally, but
reload them as latin1 if none of the "special" characters defined in 1252 but
not Latin1 are used:
if has('multi_byte')
set encoding=utf-8
setglobal fenc=latin1
" Don't detect utf-8 without a BOM by default, I don't use UTF-8 normally
" and any files in latin1 will detect as UTF. Detect cp1252 rather than
" latin1 so files are read in correctly.
set fileencodings=ucs-bom,8bit-cp1252,latin1
if has('autocmd')
augroup fenc_detect
au!
" Detect when a buffer should actually be latin1 (i.e. there are no cp1252
" bytes in the buffer). cp1252 is a superset of latin1. See
" http://en.wikipedia.org/wiki/Cp1252 for details.
"
" Since latin1 is a subset of cp1252, this does not ACTUALLY modify the
" buffer, so bypass the modifiable option.
let cp1252_latin1_diff =
\ '\u20AC'. '\u201A'. '\u0192'. '\u201E'. '\u2026'. '\u2020'. '\u2021'. '\u02C6'. '\u2030'. '\u0160'. '\u2039'. '\u0152'. '\u017D'.
\ '\u2018'. '\u2019'. '\u201C'. '\u201D'. '\u2022'. '\u2013'. '\u2014'. '\u02DC'. '\u2122'. '\u0161'. '\u203A'. '\u0153'. '\u017E'. '\u0178'
autocmd BufReadPost * let s:oldmod = &modifiable | if !s:oldmod | setlocal modifiable | endif
autocmd BufReadPost * if &fenc=~?'cp1252$' && search('['.cp1252_latin1_diff.']', 'nw') == 0 | setlocal fenc=latin1 nomodified | endif
autocmd BufReadPost * if !s:oldmod | setlocal nomodifiable | endif
augroup END
endif
endif
For some file types (notably HTML) I use the "autofenc" plugin: http://www.vim.org/scripts/script.php?script_id=2721