42

In MacOSX there's a command which can pipe the output of a command to the clipboard so that it can be pasted somewhere else in the GUI.

How can this be done from cmd.exe or with a PowerShell cmdlet?

62

Use something like:

someCommand | clip

That will pipe the result to the windows clipboard

  • Good suggestion. This is a standard Windows utility. On most systems, it's at C:\Windows\system32\clip.exe. – Nicole Hamilton Sep 11 '12 at 3:48
  • @NicoleHamilton right, but since that is part of the PATH variable, that does not need to be made explicit – soandos Sep 11 '12 at 3:50
  • My point and part of the reason I gave you an upvote was that I liked that you'd suggested a standard Windows utility, not something the OP was supposed to d/l from somewhere. Most people know about PATH. – Nicole Hamilton Sep 11 '12 at 4:25
  • 3
    For posterity, if you want the content of a file it's 'type somefile.txt | clip' – ferventcoder Feb 2 '14 at 21:10
  • 2
    Annoyingly windows echo adds a \r\n and has no echo -n ... so "echo test | clip" will have test\r\n in copy buffer ... echo|set/p="test"|clip ... addresses this gross – hpavc Feb 11 '15 at 4:11
6

I'm using the Git Bash command shell for Windows, and as someone noted above, using clip is very annoying, because it also copies the carriage return at the end of the output of any command. So I wrote this function to address it:

function cpy {
while read data; do     # reads data piped in to cpy
    echo "$data" | cat > /dev/clipboard     # echos the data and writes that to /dev/clipboard
done
tr -d '\n' < /dev/clipboard > /dev/clipboard     # removes new lines from the clipboard
}

So for example:

$ pwd | cpy  # copies directory path

$ git branch | cpy # copies current branch of git repo to clipboard
  • Good one. If you have GNU win32 utils installed you could trim out the line break with tr and still use clip, i.e: pwd | tr -d \r\n | clip – highmaintenance Apr 18 '18 at 10:07
2

Just for reference I had to copy my public key directly after Bitbucket was giving me a bad key warning. I was able to use @soandos answer like so:

cat ~/.ssh/id_rsa.pub | clip to copy my key directly from the command line on a PC. (since command line sucks compared to terminal)

0

In PowerShell, just pipe the text into Set-Clipboard. For fast typing, you can use the alias scb. This doesn't add an extra line break like the clip utility does.

For example, this command puts the contents of myfile.txt on the clipboard:

gc .\myfile.txt | scb

Note that for objects that represent file system objects, Set-Clipboard will copy the object in the same sense that Explorer does when you Ctrl+C a file. If that's not what you wanted, pipe the object through Out-String first.

0

This function replaces the standard Windows clip in Git Bash where a trailing newline is copied.

function clip {
    printf "$(</dev/stdin)" | cat > /dev/clipboard
}

This is based on Matthew's answer which only preserves the last line and introduces a trailing newline. Using printf avoids the newline that echo adds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.