Monday, November 18, 2019

Re: Trailing ^M on Windows

Hi Tony,

The thing is, :read! appears to be stripping ^M already, irrespective of the use of ++opts:

  :read !powershell.exe Get-Clipboard
  :read ++ff=unix !powershell.exe Get-Clipboard
  :read ++ff=dos !powershell.exe Get-Clipboard

To be honest, I'd expect the second one, where we specify ++ff=unix, to leave trailing ^M, but somehow that's not happening, and for your reference (after I vim -u NONE):

  :verbose set ff

Returns 'fileformat=unix'

  :verbose set ffs

Returns 'fileformats=unix,dos'

So am I correct if I say that there is something "weird" going on with system()?  I also found the following at the end of system()'s help page, but somehow the experienced behavior is not the documented one:

  To make the result more system-independent, the shell output
  is filtered to replace <CR> with <NL> for Macintosh, and
  <CR><NL> with <NL> for DOS-like systems.

I even tried to give systemlist() a go, but each entry of the array still has that trailing ^M, so it really seems like Vim cannot properly guess the fileformat from the command output.

I am really in the dark here.

Thanks,

On Sun, Nov 17, 2019 at 5:57 AM Tony Mechelynck <antoine.mechelynck@gmail.com> wrote:
On Sat, Nov 16, 2019 at 7:06 PM Matteo Landi <matteo@matteolandi.net> wrote:
>
> On 11/16, Tony Mechelynck wrote:
> >On Windows, when you read a file in Vim and every (or almost every)
> >line ends with ^M, it usually means that the last line lacks an
> >end-of-line, which made Vim read the whole file as if it were a Unix
> >file. This is how I would fix such a file:
> >
> >    :new ++ff=dos filename.ext
> >    :wq
> >
> >(this has the side-effect of opening then closing a new window for
> >that file) — see ":help ++opt".
> >Reading the file with 'fileformat' explicitly set to dos forces both
> >CR+LF or LF alone (or nothing at end of file) to be recognised as
> >ends-of-lines. Writing the file (which still has 'fileformat' set to
> >dos) writes a proper CR+LF Dos/Windows end-of-line at the end of every
> >line including the last one, so the next time you read it it will be
> >recognised as a Windows file and you won't see those pesky ^M (i.e.
> >carriage-return) characters (they are still there but they are part of
> >the normal Dos/Window end-of-line).
> >
> >Or if you want to transmit the file to be read on a Unix system, you
> >can replace :wq by :wq ++ff=unix — in that case all lines will get a
> >proper LF-only Unix end-of-line, which both Vim (on any platform) and
> >any Unix program will be able to recognise properly. In this case the
> >^M characters are not even there so no one will see them.
> >
>
> Thanks Tony for your reply, but I am afraid I did not properly explain
> myself earlier, apologies.
>
> The actual problem I dealing with is trailing ^M when reading from the
> OS clipboard; I started with dumping trailing \r\n data on a file only
> to make it easier for people to reproduce my problem, but ultimately I
> need to figure out how to read from the clipboard without having ^M
> added at the end of each line.
>
> So, going back to my experiments, can anyone suggest why, the following
> strips trailing ^M:
>
>   :read! powershell.exe Get-Clipboard
>
> While this one instead, doesn't?
>
>   :let @@ = system('powershell.exe Get-Clipboard') | exe 'normal p'
>
> Also, for those who might wonder: reading from the */+ registers
> unfortunately is not an option, as I am trying build a mini-plugin
> around "cb" (https://github.com/iamFIREcracker/cb) which is a script
> that can work via ssh too where you don't have access to the OS
> clipboard; anyway, this is the reason why I am trying to read the
> content of the OS clipboard by using a command, `powershell.exe
> Get-Clipboard`, instead of using the */+ register.
>
> Let me know if this makes things a little more clear.
>
> Thanks.
>
> --
> Matteo Landi
> https://matteolandi.net

My previous answer still applies to this usecase.

As the documentation for :read says, this command accepts a ++opt
modifier, even when used with an !external command.

I expect that

        :read ++ff=dos !powershell.exe Get-Clipboard

(with the ++ff= modifier before the exclamation mark) will give you
the result you want, without the ^M characters. Try it, then tell us
if it works.

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/CAJkCKXvV7bzJfAGK%2BhnMmwqutb-Pee3gs1yv%3DKepK7929AL%3DDg%40mail.gmail.com.

--
--
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/CAKpQHWZWnAZg86ey-1xcBhenW4tTCe8Y-vvR3DXkxS_5LYnr3A%40mail.gmail.com.

No comments: