CMD Prompt: Copying to Clipboard Adds Trailing Space

Microsoft Windows
Post Reply
User04
Posts: 2
Joined: 2022-Jan-23, 11:57 am

CMD Prompt: Copying to Clipboard Adds Trailing Space

Post by User04 »

I've noticed that, at the cmd prompt, when piping to the clipboard a trailing space is added to each line.

Compare the following two commands typed at the cmd prompt:
  1. Code: Select all

    (for %a in (red green blue) do @(echo %a)) > tmp & notepad tmp
  2. Code: Select all

    (for %a in (red green blue) do @(echo %a)) | clip
The first creates and opens the list in Notepad without any trailing spaces; the second copies the list to the clipboard with a trailing space on each line (which can be seen by pasting the result in Notepad).

Does anyone know why this happens? Better yet, does anyone know how output from stdin can be piped to the clipboard without trailing spaces?
User avatar
Simon Sheppard
Posts: 93
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: CMD Prompt: Copying to Clipboard Adds Trailing Space

Post by Simon Sheppard »

I think what is happening here is that the carriage return/newline are converted into a space

If we try this

Code: Select all

(for %a in (red green blue) do @(echo %a)) | find "e">demo.txt
it adds those spaces, the same as clip - so the problem is in the pipe not in the clip utility.

In PowerShell:

Code: Select all

PS C:\> echo "red`r`ngreen`r`nblue" | Set-clipboard
Works with no extra spaces.
Post Reply