Saturday, November 16, 2019

Trailing ^M on Windows

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.

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 cliboards are hart...\r\naren't they?\r\n" > clipboard.txt

> cat clipboard.txt
OS cliboards are hart...
aren't they?

> cat clipboard.txt -A
OS cliboards are hart...^M$
aren't they?^M$

Let's open up vim now (`vim -u NONE`) and do some experiments:

1) read 'clipboard.txt', under the cursor

:read clipboard.txt

The content is loaded, not trailing ^M, at the bottom I see:
"clpboard.txt" [dos format] 2 lines, 40 characters

2) load 'clipboard.txt' using `read!`

:read! cat clipboard.txt

The content is loaded, no trailing ^M

3) load the content into a variable, then paste it under the cursor

:let @@ = system('cat clipboard.txt') | exe 'normal p'

The content is loaded, trailing ^M are added

Let's now push the file into the OS clipboard:

> cat clipboard.txt | clip.exe

Open up vim again, and:

4) paste from the `*` register (works with `Shift+Ins` too):

:exe 'normal "*p'

OS clipboard is read, no trailing ^M

5) load the content of the clipboard using `read!`

:read! powershell.exe Get-Clipboard

The content is loaded, no trailing ^M

6) Read the content of the clipboard into a variable, then paste it under the cursor

:let @@ = system('powershell.exe Get-Clipboard') | exe 'normal p'

OS clipboard is read, trailing ^M are added

These last 2 experiments, really, left me speechless: in both cases we
are reading the output of a command, but somehow `:read` seems to strip
^M, while `system()` not. Can someone help me figure this out?

Thanks

--
Matteo Landi
https://matteolandi.net

--
--
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/20191116112441.GA12005%40hairstyle.local.

No comments: