18

I'm trying to get WSL to utilize my clipboard so I can update files from the output via xsel -b > my-umatrix-rules.txt. However, I'm getting the following error:

xsel: Can't open display: (null) : Inappropriate ioctl for device

I assume this has something to due with it being separately contained from Windows and therefore the clipboard itself. Is there a way I can get WSL to be able to look at the clipboard?

3 Answers3

20
From Windows clipboard to WSL

To get information from the Windows clipboard into WSL, use PowerShell and the Get-Clipboard cmdlet, like so:

powershell.exe -c Get-Clipboard > my-umatrix-rules.txt
From WSL to Windows clipboard

To send data to the Windows clipboard from WSL, the easiest way I know is to use clip.exe, like so:

cat my-umatrix-rules.txt | clip.exe

Note, while not applicable to this particular use-case, if you need to capture both output and error of an application in WSL to the Windows clipboard (a common scenario), just use normal Linux redirection, such as:

# <command> 2>&1 | clip.exe
ls kdkdkdkd * 2>&1 | clip.exe
NotTheDr01ds
  • 28,025
5

| clip.exe is good. However when your strings include special characters, for example Chinese, their output is weird.

Here's my aliases.

alias clip='powershell.exe -noprofile -command "chcp 65001 >\$null; clip.exe"'
alias past='powershell.exe -noprofile -command Get-Clipboard'

In clip alias, it use PowerShell and firstly change its output encoding to UTF-8(code page 65001), and then use clip.exe.

Then you can use

$ echo '测试' | clip
$ past
测试

to test.


EDIT

Besides, loading PowerShell profile costs time. As a result, I use -noprofile option to disable loading profile.

It costs about 1s before and about 0.3s after.

0

The fastest clipboard operation can be achieved using wslg and wl-clipboard.

  1. Install wl-clipboard (Eg, Ubuntu):
sudo apt install wl-clipboard
  1. Copy a string to the clipboard:
echo "A string " | wl-copy
  1. Use ctrl + v to paste.
A string 
Tushar
  • 11