19

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 4

26

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
  • For WSL, I find X410 equality good at solving the "Can't open display" error.
    – llinfeng
    Apr 5, 2020 at 17:28
  • 11
    I am having Error: Can't open display: :0 error
    – alper
    Apr 22, 2020 at 15:45
  • This worked for me on Linux Virtualbox VMs and Windows Hosts.
    – Shōgun8
    Oct 8, 2021 at 23:30
13

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

3
  • I am not sure clip.exe will work on Ubuntu (unless you using wine first no?) Oct 19, 2019 at 5:53
  • 3
    @BookOfZeus The system described in the question is Ubuntu in WSL on Windows 10. In such an environment, it is possible to run Windows programs (like clip.exe) from within the WSL system. Oct 19, 2019 at 16:01
  • Good to know! I never used windows nor never used WSL. You got my vote! Oct 19, 2019 at 22:27
1

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
  • 1
    now even better, with | tr -s '\r\n' ' ' instead of sed it wont execute commands if you accidentally copy a "Enter"
    – pt1997
    Sep 1, 2020 at 10:40
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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.