Problems to make default encoding to legacy one while encoding=utf-8

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems to make default encoding to legacy one while encoding=utf-8

adah
Currently I have lines like those follows in my _vimrc:

set encoding=utf-8
set fileencodings=ucs-bom,utf-8,chinese
au BufNewFile * setlocal fileencoding=chinese

When I use "gvim NewFileName" to open a new file, all works OK. However,
if I use "gvim" first and then use ":w NewFileName", it does not.

I tried "BufNew", but this seems not the correct thing, since it can
modify the encoding of existing files.

So my questions is: How to make the encoding of an empty buffer my chosen
one without affecting the encoding of my existing files?

Best regards,

Yongwei
Reply | Threaded
Open this post in threaded view
|

Re: Problems to make default encoding to legacy one while encoding=utf-8

A.J.Mechelynck
[hidden email] wrote:

> Currently I have lines like those follows in my _vimrc:
>
> set encoding=utf-8
> set fileencodings=ucs-bom,utf-8,chinese
> au BufNewFile * setlocal fileencoding=chinese
>
> When I use "gvim NewFileName" to open a new file, all works OK. However,
> if I use "gvim" first and then use ":w NewFileName", it does not.
>
> I tried "BufNew", but this seems not the correct thing, since it can
> modify the encoding of existing files.
>
> So my questions is: How to make the encoding of an empty buffer my chosen
> one without affecting the encoding of my existing files?
>
> Best regards,
>
> Yongwei
>
>
>

Try ":saveas NewFileName" instead of ":w NewFileName". Does it change
anything?

If it doesn't work automatically you can do it by hand: ":w
++enc=chinese NewFileName"

Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: Problems to make default encoding to legacy one while encoding=utf-8

adah
In reply to this post by adah
A. J. Mechelynck wrote:

>> So my questions is: How to make the encoding of an empty buffer my
>> chosen one without affecting the encoding of my existing files?
>
> Try ":saveas NewFileName" instead of ":w NewFileName". Does it change
> anything?

Thanks, Tony.  But this makes no difference.

> If it doesn't work automatically you can do it by hand: ":w
> ++enc=chinese NewFileName"

I know this (and "set fenc=chinese"). I need something that works by
default (like "set fenc=chinese" on an empty new buffer).

Best regards,

Yongwei
Reply | Threaded
Open this post in threaded view
|

Re: Problems to make default encoding to legacy one while encoding=utf-8

A.J.Mechelynck
[hidden email] wrote:

> A. J. Mechelynck wrote:
>
>>> So my questions is: How to make the encoding of an empty buffer my
>>> chosen one without affecting the encoding of my existing files?
>> Try ":saveas NewFileName" instead of ":w NewFileName". Does it change
>> anything?
>
> Thanks, Tony.  But this makes no difference.
>
>> If it doesn't work automatically you can do it by hand: ":w
>> ++enc=chinese NewFileName"
>
> I know this (and "set fenc=chinese"). I need something that works by
> default (like "set fenc=chinese" on an empty new buffer).
>
> Best regards,
>
> Yongwei
>
>
>

Hm. What about removing the autocommand and adding instead:

        set fileencoding=chinese

in the vimrc? For already-existing files, your 'fileencodings' (plural)
should detect Unicode files anyway, at least if they have a BOM and/or
are in UTF-8.

BTW, I use the following 'statusline' setting in order to be able to see
the current charset of each currently displayed file in the statusline
of its window. It is to be entered all on one line. (If your mail
reader, or mine, has added line breaks, it probably added them at
backslash-escaped single spaces.) The important part is between [ and ];
the rest, IIRC, just makes the default value explicit.

        set statusline=%<%f\
%h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\
%-14.(%l,%c%V%)\ %P


HTH,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: Problems to make default encoding to legacy one while encoding=utf-8

adah
In reply to this post by adah
A. J. Mechelynck wrote:

> Hm. What about removing the autocommand and adding instead:
>
>                set fileencoding=chinese
>
> in the vimrc? For already-existing files, your 'fileencodings'
> (plural) should detect Unicode files anyway, at least if they have a
> BOM and/or are in UTF-8.

Since fileencoding is a local-to-buffer option, I did not expect to
work.  However ... it works.  Thanks, Tony!

> BTW, I use the following 'statusline' setting in order to be able to
> see the current charset of each currently displayed file in the
> statusline of its window. It is to be entered all on one line. (If
> your mail reader, or mine, has added line breaks, it probably added
> them at backslash-escaped single spaces.) The important part is
> between [ and ]; the rest, IIRC, just makes the default value
> explicit.
>
>                set statusline=%<%f\
> %h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\
> %-14.(%l,%c%V%)\ %P

Good!  I will use it too.  :-)

Best regards,

Yongwei
Reply | Threaded
Open this post in threaded view
|

Re: Problems to make default encoding to legacy one while encoding=utf-8

A.J.Mechelynck
[hidden email] wrote:

> A. J. Mechelynck wrote:
>
>> Hm. What about removing the autocommand and adding instead:
>>
>>                set fileencoding=chinese
>>
>> in the vimrc? For already-existing files, your 'fileencodings'
>> (plural) should detect Unicode files anyway, at least if they have a
>> BOM and/or are in UTF-8.
>
> Since fileencoding is a local-to-buffer option, I did not expect to
> work.  However ... it works.  Thanks, Tony!
[...]
My pleasure. The reason it works (IIUC) is that all local options have a
corresponding global default. By using the above ":set" command, you set
both. When opening an empty window, the local option gets the global value.

Best regards,
Tony.