7

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?

Guitarmony
  • 73
  • 1
  • 4

1 Answers1

8
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
  • 17,574
  • 4
  • 44
  • 81
  • This is perfect, thanks. – Guitarmony Jan 18 '21 at 06:10
  • Most certainly. By the way, if you happen to be using Ubuntu and use the terminal that comes with that distro, you could run into [this issue](https://superuser.com/questions/1617959/on-wsl1-getting-clipboard-contents-using-powerhshell-changes-terminal-appearanc/1617985#1617985). Just so that you are aware of it and know that you can get around it by switching terminals. – NotTheDr01ds Jan 18 '21 at 06:23
  • I use windows terminal already, but thank you. – Guitarmony Jan 18 '21 at 07:35
  • @NotTheDr01ds I discovered that running powershell.exe within a WSL2 window changed the font. Very annoying. Your link is helpful. – Mark Ransom Jul 14 '23 at 16:50