Monday, December 7, 2020

Re: Changing encoding of an already loaded buffer

On Mon, Dec 7, 2020 at 5:40 PM A. Wik <awik32@gmail.com> wrote:
>
> Hi all,
>
> I sometimes need to change the encoding used for a file. I have the
> default set to latin1 except for files with an ucs-bom. However, when
> I load a file encoded in UTF-8 or CP-437 the default is wrong. What I
> do then is normally to ":set fencs=utf8" and ":vi" to reload the file.
>
> However, what can I do about a file that cannot be reloaded? Eg:
>
> $ man llseek | gvim -f -
>
> To work around it, I have to do this:
>
> $ man llseek > llseek.man
> $ gvim llseek.man
>
> Is there another way?
>
> Regards,
> Albert.

If you find out after loading the stdin that it was opened in the
wrong encoding, then it's too late; but if you know the file's
encoding in advance, the should be a way, especially if your
'encoding' (the charset used internally by Vim) is UTF-8 and if your
Vim is compiled with +iconv.

To be able to detect Latin1 and UTF-8 (and UTF-16 with BOM) automagically, add
set fileencodings=ucs-bom,utf-8,latin1
somewhere in your vimrc (the s at the end of fileencodings is
important); but this isn't enough for files in cp437, especially if
Vim gets them on stdin. For those, load them with (untested)
someprogram | view ++enc=cp437 -
(the minus sign at the end is important) which means that you have to
know the file's encoding before starting Vim if it is other than UTF-8
or Latin1. Using "view" instead of "vim" on the command-line avoids
problems with the 'modified' flag; for ++enc see ":help ++enc".

The above will detect files in 7-bit us-ascii encoding as utf-8 rather
than Latin1. This is not a bug, because the 128 characters which are
valid in us-ascii are represented identically in all three in
us-ascii, Latin1 and UTF-8.

Best regards,
Tony.

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXtd5YiRQv3wa7GAOwy%3Dq9P1zcGKv0rgQRpr1sw2qO2A0Q%40mail.gmail.com.

No comments: