익명 03:11

Use clipboard through WSL?

Use clipboard through WSL?

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?



Top Answer/Comment:

Update Note: When I originally wrote this answer in 2021, WSL properly handled setting the encoding when redirecting/piping to the clip.exe command. Unfortunately, something changed in WSL 1.1.2 (see my GitHub issue) and "special characters" no longer copied (or at least pasted) correctly using a "pure" clip.exe solution.

There are several solutions to this issue, but at this point, for those using POSIX shells in WSL, I recommend @PilgrimLyieu's answer.

For those using Nushell, see clip copy further down in my answer ...

Other POSIX solutions are listed below, but should mostly be superseded by PilgrimLyieu's aliases.


From Windows clipboard to WSL (POSIX-like shells)

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 (Simple-case, POSIX-like Shells)

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

clip.exe < my-umatrix-rules.txt
From WSL to Windows Clipboard (non-standard characters, any shell)

A few years ago (2023) it appears that something changed in WSL that no longer allowed it to properly set the codepage for the clipboard when piping to clip.exe (see this GitHub issue). Line-drawing characters, umlauts, and other UTF8 characters outside the normal ASCII or LATIN1 codepage would become garbled when pasted from the clipboard after being placed there by clip.exe (at least from WSL).

For example:

echo "╭────────────" | clip.exe
powershell.exe -c "Get-Clipboard"
# => Γò¡ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ

echo "aöüß" | clip.exe
powershell.exe -c "Get-Clipboard"
# => aöüß

There are several possible solutions here. First, see @PilgrimLyieu's solution. That's probably the best general-purpose solution to WSL's copy-issues at this point.

Second, (credit @Panki from edit) if you know the encoding of the source characters, you can use iconv to set the format. For instance, for the aöüß case above, the characters are from UTF-16LE:

echo "aöüß" | iconv -t UTF-16le | clip.exe
# or, if using the file example above ...
iconv -t utf-16le < my-umatrix-rules.txt | clip.exe

And a third option is one I used for a while, but is probably suboptimal compared to PilgrimLyieu's answer -- Instead of providing clip.exe with standard-input, you can use an argument to PowerShell's Set-Clipboard cmdlet from within WSL. For example:

pwsh.exe -c "Set-Clipboard @'
aöüß
'@"

If using a file, you'll need to read the file from within PowerShell, e.g.:

pwsh.exe -c "Get-Content -Raw '\\\\wsl$\\distroname\\home\\usesrname\\filename.txt' | Set-Clipboard"
Nushell

I'm mostly using Nushell nowadays (disclaimer, I'm a maintainer), and honestly I haven't thought too much about the encoding issues recently since this is handled automatically in Nushell's clipboard support:

use std/clip
'╭────────────' | clip copy

# or file-based
open my-umatrix-rules.txt | clip copy

Additional Scenarios (POSIX-like shells)

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
상단 광고의 [X] 버튼을 누르면 내용이 보입니다