52

How do I copy results from the commandline directly to the clipboard?

On Windows's cmd.exe I can do simply echo "asd" | clip and it pipes output to the clipboard.

I tried to install xclip for that, and though it compiled, when called it prints:

Error: Can't open display: (null)

Using mouse is not the solution.

Marek Lewandowski
  • 3,291
  • 1
  • 20
  • 26

4 Answers4

93

In Build 14393 or later, if you want to copy something to clipboard in WSL console, just add '.exe' to what you do in Windows cmd.

echo "aaa"|clip.exe

To read from clipboard:

powershell.exe -command "Get-Clipboard"
reker
  • 2,063
  • 13
  • 12
  • Cool, works out of the box after upgrading to WSL/Ubuntu 16! – Marek Lewandowski Apr 15 '17 at 22:28
  • 3
    `echo` didn't work for me, but `cat` did. That is, entering `cat filePath | clip.exe` in the command prompt. – Jet Blue Feb 08 '18 at 03:21
  • 1
    this works from shell to clipboard, but is there a solution for the reverse? looking for `pbpaste` or `xclip -o` equivalent. – rev Mar 21 '18 at 01:34
  • 1
    @JetBlue Sounds like you were using echo wrong, then -- echo just prints the text you give it to the output. `cat` reads a file. – Nic Nov 10 '18 at 02:14
  • 1
    Something strange happens when I try to use io redirection with the read from clipboard option. I can run it just fine and it will dump the clipboard contents into the command line just fine, but when I try to do e.g. `powershell.exe -command "Get-Clipboard" > test.txt`, my WSL window appears to lose all personalization settings (font size, typeface) except color scheme. Is this typical? – R. Barrett Jan 20 '21 at 23:11
8

In order to copy non-ascii characters (other languages), I had to do this:

echo 'αβψδεφγ' | iconv -f utf-8 -t utf-16le | clip.exe

utf-16le excludes the preceeding BOM so you can paste it back

Chris
  • 738
  • 8
  • 11
  • I'm unable to run PowerShell scripts on my computer, so I had created a bash script instead. I tried running PowerShell from within the bash script but failed to get the results I wanted, and this solution was the only one that worked. – purplecat Mar 11 '20 at 20:17
  • Thanks for sharing! I created an alias for this in my WSL `~/.bashrc` file: `alias clip='iconv -f utf-8 -t utf-16le | clip.exe'` Now I can pipe output to it like `echo 'αβψδεφγ' | clip` – piercebot May 21 '20 at 00:41
  • Perfect, exactly what I needed to get this working on Cygwin. – Hashim Aziz Jan 31 '21 at 00:54
1

If you would like to have something more 'easy' to use, you can add the commands from @reker to your ~/.bashrc file (if you use zsh, you have to put it in the ~/.zshrc file).

I added these two lines to my file:

alias paste="powershell.exe -command \"Get-Clipboard\""
function clip { "$1" | clip.exe;}

I use clip as a function, so I can use the command linux like ('command' 'use this for command'). If you would prefer the alias way, you can add something like

alias clip=clip.exe

than you don't have to write the .exe all the time.

Don't forget to run the command

source ~/.zshrc

after you saved the file. Otherwise the changes are only applied after a restart of your console.

Romanicus
  • 401
  • 4
  • 4
  • I was looking strictly for copying the output of a command as I do that a lot. I never used a paste using command (I always just do shift+insert). But that's a nice suggestion, thanks! – Marek Lewandowski Mar 14 '20 at 00:55
  • There are a few bugs.So you can't copy&paste a path when there is a whitespace in it (using pwd). It doesn't escape them. – Romanicus Mar 16 '20 at 09:28
0

This still does not seem to be supported: Can Bash on Windows interact with the system clipboard?.

A clever workaround is the open source tool plak.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90