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.
Archived Comments
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.
You are most welcome - I'm happy this helped!
This is really helpful, thank you! :)
You are welcome. I had to use it myself a day or so ago.
Very helpful! How to paste the content at command line, i.e. in bash programs?
Maik
I believe right click will do it.
Thanks a lot Raymond.
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 commandSet-Clipboard
command.xclip
shall over the capability to work with both pure text and rich-text contents.Thank you thank you!
Any idea how to copy text from vim using wsl bash?
I use Windows Terminal (the new one) and it supports copying from it very easily. You just CTRL+C.
nicee. How can I get that? is it officially released?
You can get it at the MS App Store, or GitHub too - https://github.com/microsof...
Thanks!!
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...
You just saved my days...thanks for sharing!!