How can I get around using xclip in the Linux subsystem on Win 10?

On a Linux subsytem on Windows 10 you get an error when using xclip, ex:

cat some/file.txt | xclip
Error: Can't open display: (null)

There is no Desktop (UI), so of course there isn't a clipboard to be used. Could there be a workaround to make it work with the Windows clipboard?

4 Answers

See WSL issue #1069 for two workarounds:

Just to clarify for anyone who stumbles upon this thread, to use type cat filePath | clip.exe into the command prompt.
- JetStarBlues commented on Feb 8

Or:

I can confirm that having Xming running and configuring the DISPLAY env var is enough to make something go to the windows clipboard:

$ export DISPLAY=:0
$ echo 'some text' | xclip -selection clipboard

This works just fine. Apparently using the primary clipboard also works fine, so I'm guessing Xming will redirect both to the windows clipboard.
- mateusmedeiros commented on Sep 9, 2016

3

clip.exe < ~/.ssh/id_rsa worked for me to copy my ssh keys to github on WSL2.

3

I'm using temux all the time, adding this to my .tmux.conf enables me to copy and past without installing an X server :)

#to be able to use mouse buttons and scroll:
set -g mouse on
#to copy to Windows clipboard by marking text
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel clip.exe
#to be able to paste by right clicking (like in default Windows Terminal)
unbind-key MouseDown3Pane
bind-key -n MouseDown3Pane run "tmux set-buffer \"$(powershell.exe -command Get-Clipboard | tr -s '\r\n' ' ')\"; tmux paste-buffer"
1

You can create a file named, say "pbcopy" and fill it with

#!/usr/bin/bash
FILE=$(pwd)/$1
cat "${FILE}" | clip.exe

Then move the file to your bin directory.

mv pbcopy ~/bin

Now you can use pbcopy on your wsl to copy file contents to clipboard.

pbcopy my/file/path

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like