22

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?

5 Answers 5

30

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

5
  • For WSL, I find X410 equality good at solving the "Can't open display" error.
    – llinfeng
    Commented Apr 5, 2020 at 17:28
  • 15
    I am having Error: Can't open display: :0 error
    – alper
    Commented Apr 22, 2020 at 15:45
  • This worked for me on Linux Virtualbox VMs and Windows Hosts.
    – Shōgun8
    Commented Oct 8, 2021 at 23:30
  • clip.exe works perfect! for more information on the usage type clip.exe /?
    – tturbo
    Commented Dec 1, 2023 at 8:08
  • And to get the clipboard content inside WSL: powershell.exe Get-Clipboard
    – Nino Filiu
    Commented Feb 20 at 13:05
15

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?) Commented Oct 19, 2019 at 5:53
  • 4
    @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. Commented Oct 19, 2019 at 16:01
  • Good to know! I never used windows nor never used WSL. You got my vote! Commented 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
    Commented 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
1

I have been looking through heaps of posts around copying to clipboard and pasting from clipboard in WSL Ubuntu terminal.

Working with clipboard in WSL

Best way to copy to clipboard is:

echo 'something' | clip.exe

Best way to paste:

powershell.exe Get-Clipboard

Using aliases for simplification

You can even put aliases in .bashrc or .bash_aliases to make it similar to Mac bash terminal:

alias pbcopy='clip.exe'
alias pbpaste='powershell.exe Get-Clipboard'

Then commands to copy from pipe would be:

echo "hello" | pbcopy

To paste to file would be:

pbpaste > pasted.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .