On 25/10/09 04:35, Alessandro Antonello wrote: > > Hi, guys. > > I want to thank you all the ideas. My current setting of 'fenc' is: > > fencs=ucs-bom,latin1,utf-8,utf-16le > fenc=latin1 bomb > > This works fine when I have two or more files opened in VIM with latin1 and > utf-16 encodings. When I switch out and back utf-16 files VIM recognizes then > correctly. But this does not work wit utf-8 and latin1 encodings. Off course, > all utf-8 characters are valid latin1 characters so, VIM does not know that is > a utf-8 file and uses latin1. > > I'm currently loading the file with ':e ++enc=utf-8', with also works fine. > But, when I need to jump out of this buffer and later come back, the encoding > is setted by VIM as latin1. There is a way to tell VIM that it's a utf-8 > file and when I jump out of the buffer and later come back this setting > remains unchanged? > > Best regards, > Alessandro Antonello
As long as the file contains only ASCII text, i.e., bytes lower than 0x80, it doesn't matter whether you edit it as Latin1 or UTF-8 because _for ASCII text_ these two encodings are the same. If you know that the file will someday contain non-ASCII text, you have to make sure that Vim will store it correctly the first time it contains non-ASCII text. In most (but not all) cases, you can do this safely by acting in advance: 1) If you want the file to be handled as UTF-8, and the file is not of a kind (such as any executable starting in #! ) where BOMs cannot be used, then you can ":setlocal bomb fenc=utf-8" and, assuming that 'encoding' is set to utf-8 and that 'fileencodings' starts with "ucs-bom", the file (which isn't pure-ASCII anymore thanks to the BOM) will always be recognized as UTF-8 from then on. 2) If you want the file to be handled as Latin1, you may intentionally insert some non-ASCII characters (for instance by underlining your top title with "divide-by" signs instead of hyphens), then ":setlocal fenc=latin1" and from then on, the file (which isn't pure-ASCII anymore thanks to the non-ASCII Latin1 characters) will be recognized as Latin1. (Note: the 'bomb' setting is irrelevant when 'fileencoding' is Latin1.) 3) One problem is that of files where you cannot afford a BOM, but which you still want to handle as UTF-8. One possible solution is to act in a manner similar to (2) above: add some non-ASCII characters somewhere in the body of the text (for instance, if that file is a bash or perl script, in a comment), then immediately ":setlocal fenc=utf-8 nobomb", and the file (which isn't pure-ASCII anymore thanks to the non-ASCII UTF-8 codepoints) will be recognized as UTF-8 from then on (provided of course that 'encoding' is set to UTF-8 and that 'fileencodings' includes "utf-8" early enough, and in any case before any 8-bit encoding). So as a summary: - Set 'encoding' to utf-8 - Set 'fileencodings' [plural] to (for example) ucs-bom,utf-8,latin1 - Make sure that you set the 'fileencoding' [singular] of a file to either UTF-8 or Latin1 the _first_ time you write non-ASCII data into the file, which in most cases can be done in advance as shown above. - If you miss the first time (and forget to act), then as long as the (now non-pure-ASCII) file contains nothing above 0xFF it can be represented as either UTF-8 or Latin1 but not both at the same time. You can "change your mind" then by means of either ":setlocal fenc=latin1" or ":setlocal fenc=utf-8" and that file's new setting will be kept from then on. Best regards, Tony. -- A child of five could understand this! Fetch me a child of five. --~--~---------~--~----~------------~-------~--~----~ You received this message from the "vim_use" maillist. For more information, visit http://www.vim.org/maillist.php -~----------~----~----~----~------~----~------~--~---
