On 23/09/09 07:56, pansz wrote:
>
> Tom Link ๅ†™้“:
>> Hi,
>>
>> Is there a way to set the fenc option from an autocommand so that I
>> would get the same effect as if I opened the file with :edit +
>> +enc=utf8 FILENAME
>>
>> The background: I usually use (f)enc=latin1 but would like to edit the
>> files in a certain directory with fenc=utf8. I currently reopen the
>> file with the above :edit command but I'm certain there is a better
>> solution. Any ideas?
>>
>> Regards,
>> Thomas
>
> In order to be able to edit different file encodings you must set the
> 'fencs' option. The 'fencs' option specifies encodings to try when
> opening an existing file, you can set it to :set
> fencs=ucs-bom,utf-8,latin1, this auto-detection works for existing file.
>
> for the new file, the encoding will be set to 'enc', so it would
> generally be in utf-8, if you want to save the file in latin1, simply
> :set fenc=latin1 before you save the file. and the file will be saved as
> latin1. next time when you open the file it will be recognized as latin1.
>
> if your file contains only ascii characters, it will be detected as
> utf-8, will in most cases doesn't matter too much, you can always change
> the file encoding on-the-fly.

Yes, and I'll add the following "tips and tricks":

- you can set defaults for newly created (empty) files with 
":setglobal": for instance if you use ":setglobal fenc=latin1 bomb" in 
your vimrc, newly created files for which you don't set 'fenc' to 
something else will be recorded in Latin1, and those for which you 
specify a Unicode encoding will have a BOM by default. (The 'bomb' 
option has no action on files in non-Unicode encodings such as Latin1.)

- If you want one particular file to be recognized as UTF-8 not only by 
Vim but also by other programs (let's say by other Windows editors such 
as WordPad; or by browsers if the files are in HTML, CSS or even 
plaintext) it helps if you use ":setlocal bomb" (or maybe ":setlocal 
fenc=utf-8 bomb") before saving the file. Note that the BOM consists of 
bytes with the high bit set, so the following paragraph never applies to 
such a file.

- As long as a file contains only 7-bit ASCII, it doesn't matter whether 
Vim "sees" it as Latin1 or UTF-8, because US-ASCII, Latin1 and UTF-8 all 
map the same 128 characters to the same bytes in the same order from 
0x00 to 0x7F inclusive.

- If you want to make sure that Vim recognizes one particular file as 
Latin1, make sure that it contains at least "some" characters above 
0x7F: this will be easy if the file is in a language where upper-ASCII 
characters are frequent, such as French, Spanish, German or Icelandic; 
but even if it is in English with not a single accented letter, you can 
for instance underline your top title with "divided-by" signs รท, which 
can be entered in Vim Insert or Command-line modes as any of:
        Ctrl-K - :      see :help digraph.txt, and in particular
                        |digraphs-use| and |digraph-table|
        Ctrl-V 247      see :help i_CTRL-V_digit
        Ctrl-V o367     ditto
        Ctrl-V xF7      ditto
, where the spaces are only for clarity, don't hit the space bar there; 
then immediately use ":setlocal fenc=latin1" so those bytes you just 
added in the range [0x80-0xFF] won't be saved as multibyte UTF-8 codes 
by mistake.


Best regards,
Tony.
-- 
hundred-and-one symptoms of being an internet addict:
162. You go outside and look for a brightness knob to turn down the sun.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to