Marek Lewandowski
Marek Lewandowski

Reputation: 3401

Copy to clipboard using Bash for Windows

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.

Upvotes: 54

Views: 29199

Answers (4)

AAugustin
AAugustin

Reputation: 1

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.

Upvotes: 0

Romanicus
Romanicus

Reputation: 421

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.

Upvotes: 1

reker
reker

Reputation: 2193

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"

Upvotes: 98

Chris
Chris

Reputation: 759

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

Upvotes: 8

Related Questions