Hello everyone,
I have been battling with trailing ^M on Windows, and I was wondering if
any of you could help me figure it out. I already posted this on
vim_use, but unfortunately I could not get to the bottom of it, so here
I am posting it on vim_dev.
Long story short:
1) I am the maintainer of a small cross-platform script, cb [1], that
simplifies the access to the OS cliboard (i.e. a glorified wrapper of
tools lile pbcopy, pbpaste, clip.exe, xclip)
2) I am also the maintainer a small vim plugin, vim-cb [2], which aims
to expose few nmap expressions to easily read/write to the OS clipboard
using `cb`
3) When I paste multi-line text on Windows, I end up with a bunch of
trailing ^M
All right, let's create a file with `\r\n`s (that's how the clipboard
appears to be populated when I copy things from Chrome):
> echo -en "OS clipboards are hart...\r\naren't they?\r\n" >
> clipboard.txt
> cat clipboard.txt
OS clipboards are hart...
aren't they?
> cat clipboard.txt -A
OS clipboards are hart...^M$
aren't they?^M$
Next, copy that file into the OS clipboard, and confirm \r\n are still
there:
> cat clipboard.txt | clip.exe
> powershell.exe Get-Clipboard | cat
OS clipboards are hart...
aren't they?
> powershell.exe Get-Clipboard | cat -A
OS clipboards are hart...^M$
aren't they?^M$
Now let's open up vim (`vim -u NONE`), and try to read the OS clipboard:
1) load the OS clipboard into a variable using `system()`, then paste it
under the cursor
:let @@ = system('powershell.exe Get-Clipboard') | exe 'normal p'
The content is loaded, trailing ^M are added
2) load the content of the clipboard using `read!`
:read! powershell.exe Get-Clipboard
The content is loaded, without trailing ^M
On vim-use I was suggested that I use ++opts, but it doesn't seem to
matter, at all.
3) `read!` with `++ff=dos`
:read ++ff=dos !powershell.exe Get-Clipboard
Te content is loaded, without trailing ^M (this is expected, as we are
forcing a setting that Vim would have figured out on its own anyway)
4) `read!` with `++ff=unix`
:read ++ff=dos !powershell.exe Get-Clipboard
Te content is loaded, without trailing ^M (this is *not* expected, as
the forced setting is different from the one Vim would have guessed by
looking at the output of the command)
I was also looking at the man page for `system()`, and it seems it
should automatically handle different types of EOL, but somehow it's not
happening:
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 am really in the dark here, so I wonder if anyone could help me
understand what's going on?
Thanks in advance.
PS. I am experiencing the same on MacOS (simply replace clip.exe with
pbcopy, and powershell.exe... with pbpaste).
PPS. For the time being I am calling `substitute()` on the paste text to
manually strip \r\n, but I was hoping I could rely on Vim for that
[1]
https://github.com/iamFIREcracker/cb
[2]
https://github.com/iamFIREcracker/cb.vim
--
Matteo Landi
https://matteolandi.net