Copying to Clipboard with Windows Subsystem for Linux

This post is more than 2 years old.

This tip is 100% thanks to Ben Hillis, a developer working on the Windows Subsystem for Linux (WSL). Yesterday I needed to copy a file under WSL to the my system clipboard. If you Google for how to do this, you'll see a CLI called clip that works under Unbuntu, however, that doesn't work under WSL. If I had to guess I'd say because there's isn't a GUI involved with WSL but to be honest, I'd be guessing.

When I asked on Twitter, Ben had a simple solution - use clip.exe. I keep forgetting that WSL provides complete access to Windows executables. I knew this - heck - it's how my tip on loading VSCode Insiders from WSL worked. But I didn't even think to check if Windows had a utility to copy to the clipboard.

In case you're curious, this is how you would copy a file under WSL to your Windows clipboard:

cat report.txt | clip.exe

And I'm sure there's numerous other ways too. Anyway, I'm mainly just blogging this so I don't forget.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Christopher Patti posted on 7/8/2018 at 12:50 AM

Thank you this helps a LOT! I've recently started using Windows 10 WSL via an AWS workspace at work, and it's almost a workable environment :) (I'm much more used to working on a Mac).

The only sticking point still is cutting and pasting large amounts of text. ConsEmu is pretty good in general terms as a terminal, but it still falls short in that regard. With your tip, I can work around this by simply using screen to capture log files, copy them back to my Windows workstation, and then using clip.exe to get them into the clipboard where they can then be pasted where they need to go.

Comment 2 (In reply to #1) by Raymond Camden posted on 7/14/2018 at 3:08 PM

You are most welcome - I'm happy this helped!

Comment 3 by James Drever posted on 9/5/2018 at 1:29 PM

This is really helpful, thank you! :)

Comment 4 (In reply to #3) by Raymond Camden posted on 9/5/2018 at 1:42 PM

You are welcome. I had to use it myself a day or so ago.

Comment 5 by MaikMaalig posted on 12/14/2018 at 4:36 PM

Very helpful! How to paste the content at command line, i.e. in bash programs?
Maik

Comment 6 (In reply to #5) by Raymond Camden posted on 12/14/2018 at 4:43 PM

I believe right click will do it.

Comment 7 by mv posted on 12/15/2018 at 11:21 AM

Thanks a lot Raymond.

Comment 8 by Linfeng Li posted on 4/5/2020 at 6:54 PM

Another possibly is to get a X-server running and use xclip. To get the contents to reach system clipboard, the following statement worked on WSL: echo "Hi" | xclip -selection clipboard. (Source for the -selection flag)
clip.exe only handles plain text, and there are limitation for the PowerShell command Set-Clipboard command. xclip shall over the capability to work with both pure text and rich-text contents.

Comment 9 (In reply to #3) by Dylan posted on 5/18/2020 at 7:08 PM

Thank you thank you!

Comment 10 by xanderjakeq posted on 5/19/2020 at 3:02 PM

Any idea how to copy text from vim using wsl bash?

Comment 11 (In reply to #10) by Raymond Camden posted on 5/19/2020 at 3:04 PM

I use Windows Terminal (the new one) and it supports copying from it very easily. You just CTRL+C.

Comment 12 (In reply to #11) by xanderjakeq posted on 5/19/2020 at 3:16 PM

nicee. How can I get that? is it officially released?

Comment 13 (In reply to #12) by Raymond Camden posted on 5/19/2020 at 3:19 PM

You can get it at the MS App Store, or GitHub too - https://github.com/microsof...

Comment 14 (In reply to #13) by xanderjakeq posted on 5/19/2020 at 3:28 PM

Thanks!!

Comment 15 by Dave Kerr posted on 7/27/2020 at 3:45 PM

Thanks for the tip! For anyone wanting to paste as well, I use this:


alias pbcopy="clip.exe"
alias pbpaste="powershell.exe -command 'Get-Clipboard' | sed -e 's/\r\n$//g'"

I wrote up details here. https://effective-shell.com...

Comment 16 by Deepak posted on 10/21/2020 at 7:54 AM

You just saved my days...thanks for sharing!!