Tuesday, December 8, 2020

Re: Changing encoding of an already loaded buffer

Albert Wik wrote:

> > > Right. The only way I've found is to use a temporary file.
> > > Incidentally, the zsh shell makes that easy:
> > > % gvim -f =(man llseek)
> >
> > Assuming that loading the text as latin1 didn't mess it up (since it's
> > an 8 bit encoding it should be OK), then you can convert it to utf-8
> > with:
> > :set fencs=utf-8,latin1
> > :%!iconv -f latin1 -t utf-8
> >
> > Vim might recognize the utf-8 encoding, if not set set 'fenc':
> > :set fenc=utf8
> >
> > Hopefully that works.
>
> Thanks a lot for the "%!"-idea! That's what I needed.
>
> This works:
> :set fencs=utf8
> :%!cat
> although "fenc" remains "latin1".

Yeah, for an existing buffer and filtering the first entry in 'fencs' is
used to read the filter output, but 'fenc' isn't set. That's a bit
strange, but I'm not sure what would break if we change this. It might
actually be good to fix this, since if you write that file it might get
messed up.

> It is not appropriate to use "iconv -f latin1 -t utf8" (that does in
> fact corrupt the data!) because the data is already in UTF-8, and that
> is why it is not displayed properly in Vim (because Vim thinks it is
> in Latin-1); in particular, the short dash character is shown as
> "â<80><90>". When it is displayed properly, a "‐" is shown; putting
> the cursor at it and doing "ga" reports that this is character number
> 0x2010.
>
> Why does "set fencs=utf8" matter for the "%!cat" operation if Vim is
> not going to change the "fenc" accordingly?

When reading a file (or filter output) the values in 'fencs' are tried
one by one. Normally when something fails then the next one is tried,
but since reading filter output from a pipe doesn't allow for a retry,
it will always use the first one.

The real problem is that 'fencs' was set to "latin1" at first, thus Vim
didn't even try to use another encoding. Perhaps it also works if you
do that on the command line:
somecommand | vim - -c 'set fencs=utf8,latin1'

Didn't try it. Should at least work if you set 'fencs' in your .vimrc.


--
If an elephant is left tied to a parking meter, the parking fee has to be paid
just as it would for a vehicle.
[real standing law in Florida, United States of America]

/// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

--
--
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/202012081647.0B8GlVCw1678686%40masaka.moolenaar.net.

No comments:

Post a Comment