Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copying and pasting from system clipboard using something like xclip #1069

Closed
brandonpollack23 opened this issue Sep 8, 2016 · 38 comments
Closed

Comments

@brandonpollack23
Copy link

In an application like neovim, you can use xclip as a clipboard provider (you can also use xclip in general which is useful) to copy and paste from the system clipboard. As there is no X server here, and windows is the shell, it'd be great if we had an alternative.

a version of xclip running that messages to the windows shell for copy/paste would be amazing.

I am running the latest consumer release of RS

probobly not useful but here is a strace of xclip
execve("/usr/bin/xclip", ["xclip"], [/* 24 vars */]) = 0
brk(0) = 0x12c8000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7a4afd0000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=40023, ...}) = 0
mmap(NULL, 40023, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7a4afd6000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libXmu.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200g\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=103016, ...}) = 0
mmap(NULL, 2199416, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a4a9e0000
mprotect(0x7f7a4a9f8000, 2093056, PROT_NONE) = 0
mmap(0x7f7a4abf7000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x7f7a4abf7000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libX11.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360\207\1\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1265072, ...}) = 0
mmap(NULL, 3362112, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a4a6a0000
mprotect(0x7f7a4a7d0000, 2097152, PROT_NONE) = 0
mmap(0x7f7a4a9d0000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x130000) = 0x7f7a4a9d0000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P \2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1840928, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7a4afc0000
mmap(NULL, 3949248, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a4a2d0000
mprotect(0x7f7a4a48a000, 2097152, PROT_NONE) = 0
mmap(0x7f7a4a68a000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ba000) = 0x7f7a4a68a000
mmap(0x7f7a4a690000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7a4a690000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libXt.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\2601\1\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=413232, ...}) = 0
mmap(NULL, 2511648, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a4a060000
mprotect(0x7f7a4a0bf000, 2097152, PROT_NONE) = 0
mmap(0x7f7a4a2bf000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5f000) = 0x7f7a4a2bf000
mmap(0x7f7a4a2c5000, 800, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7a4a2c5000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libXext.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\2005\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=73288, ...}) = 0
mmap(NULL, 2169048, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a49e40000
mprotect(0x7f7a49e51000, 2093056, PROT_NONE) = 0
mmap(0x7f7a4a050000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x10000) = 0x7f7a4a050000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libxcb.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \226\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=125392, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7a4afb0000
mmap(NULL, 2220648, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a49c20000
mprotect(0x7f7a49c3d000, 2097152, PROT_NONE) = 0
mmap(0x7f7a49e3d000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1d000) = 0x7f7a49e3d000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\16\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=14664, ...}) = 0
mmap(NULL, 2109744, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a49a10000
mprotect(0x7f7a49a13000, 2093056, PROT_NONE) = 0
mmap(0x7f7a49c12000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f7a49c12000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libSM.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\33\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=30896, ...}) = 0
mmap(NULL, 2126160, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a49800000
mprotect(0x7f7a49807000, 2093056, PROT_NONE) = 0
mmap(0x7f7a49a06000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x7f7a49a06000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libICE.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260N\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=98288, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7a4afa0000
mmap(NULL, 2208000, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a495e0000
mprotect(0x7f7a495f7000, 2093056, PROT_NONE) = 0
mmap(0x7f7a497f6000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7f7a497f6000
mmap(0x7f7a497f8000, 12544, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7a497f8000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libXau.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\16\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=14456, ...}) = 0
mmap(NULL, 2109720, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a493d0000
mprotect(0x7f7a493d2000, 2097152, PROT_NONE) = 0
mmap(0x7f7a495d2000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f7a495d2000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libXdmcp.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\23\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=22616, ...}) = 0
mmap(NULL, 2117856, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a491c0000
mprotect(0x7f7a491c5000, 2093056, PROT_NONE) = 0
mmap(0x7f7a493c4000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x4000) = 0x7f7a493c4000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libuuid.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\26\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=18936, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7a4af90000
mmap(NULL, 2113968, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7a48fb0000
mprotect(0x7f7a48fb4000, 2093056, PROT_NONE) = 0
mmap(0x7f7a491b3000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x7f7a491b3000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7a4af80000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7a4af70000
arch_prctl(ARCH_SET_FS, 0x7f7a4af70780) = 0
mprotect(0x7f7a4a68a000, 16384, PROT_READ) = 0
mprotect(0x7f7a491b3000, 4096, PROT_READ) = 0
mprotect(0x7f7a493c4000, 4096, PROT_READ) = 0
mprotect(0x7f7a495d2000, 4096, PROT_READ) = 0
mprotect(0x7f7a497f6000, 4096, PROT_READ) = 0
mprotect(0x7f7a49a06000, 4096, PROT_READ) = 0
mprotect(0x7f7a49c12000, 4096, PROT_READ) = 0
mprotect(0x7f7a49e3d000, 4096, PROT_READ) = 0
mprotect(0x7f7a4a9d0000, 4096, PROT_READ) = 0
mprotect(0x7f7a4a050000, 4096, PROT_READ) = 0
mprotect(0x7f7a4a2bf000, 4096, PROT_READ) = 0
mprotect(0x7f7a4abf7000, 4096, PROT_READ) = 0
mprotect(0x604000, 4096, PROT_READ) = 0
mprotect(0x7f7a4ae22000, 4096, PROT_READ) = 0
munmap(0x7f7a4afd6000, 40023) = 0
brk(0) = 0x12c8000
brk(0x12e9000) = 0x12e9000
write(2, "Error: Can't open display: (null"..., 34Error: Can't open display: (null)
) = 34
exit_group(1) = ?
+++ exited with 1 +++

xclip is needeed for this so sudo apt install xclip

@aseering
Copy link
Contributor

aseering commented Sep 8, 2016

Hi @brandonpollack23 -- good point; this would be useful. I wonder if xclip can be made to work if you install an X server and configure WSL to use it: http://wsl-forum.qztc.io/viewtopic.php?f=6&t=14

@mateusmedeiros
Copy link

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.

Can't say for sure about the other X implementations, though, but I think redirecting the X clipboard to the windows clipboard is the natural thing to do for any X windows implementation.

@whorfin
Copy link

whorfin commented Sep 12, 2016

Xming does indeed work, but I'd like to suggest that the need to support "the home OS [ie Windows] clipboard" without X11 is still valid.

It's arguably a gross hack, but the cygwin method of "/dev/clipboard" to read and write the system clipboard would be very groovy.

@iogbole
Copy link

iogbole commented Sep 13, 2016

+1

@x77686d
Copy link

x77686d commented Dec 1, 2016

+10!

Something like Cygwin's /dev/clipboard would be great but something as simple as OS X's pbcopy and pbpaste would be plenty.

@mogest
Copy link

mogest commented Dec 8, 2016

In recent insider builds, WSL has been able to access Windows binaries. Thanks to this, you can use clip.exe from bash to copy stuff into the windows clipboard.

@laktak
Copy link

laktak commented Dec 10, 2016

@mogest I tried this with vim-fakeclip but it can only write and not read.

A workaround is to use PowerShell but that's really slow (it takes 1-2 seconds for powershell.exe -Command Get-Clipboard).

@mateuszlewko
Copy link

Hey Guys!
Do you know how to use this clip.exe tool with tmux?
Thanks!

@tulsileathers
Copy link

  • 1

@eredi93
Copy link

eredi93 commented Jan 3, 2018

is anyone working on this?

@JetStarBlues
Copy link

Just to clarify for anyone who stumbles upon this thread, to use type cat filePath | clip.exe into the command prompt.

@HiImJulien
Copy link

I am not sure how update-to-date this is. But "clip.exe" is not working for me...
It says "clip.exe command not found"

@JetStarBlues
Copy link

@HiImJulien Just tested it, still works. Maybe it's the version of WSL you are running?

@JetStarBlues
Copy link

JetStarBlues commented Apr 28, 2018

@HiImJulien Try typing notepad.exe in WSL, does notepad open up? If not, the culprit is likely your WSL version. You can also try typing clip.exe on the regular windows command prompt to make sure it's in your path (it should be by default).

@therealkenc
Copy link
Collaborator

The OP here didn't really lay out a well defined ask. There is no clipboard in Linux outside of the context of X11 (or another windowing system). If you run xclip (which is an old-school X11 client application) communicating with a well behaved X server, it will grab the X clipboard as advertised. How the X server clipboard behaves is a function of the X server, and not a WSL actionable. As for the Windows clipboard, that is clip.exe via interop.

@laktak
Copy link

laktak commented May 25, 2018

As for the Windows clipboard, that is clip.exe via interop.

@therealkenc clip.exe can only copy but not paste.

@therealkenc
Copy link
Collaborator

therealkenc commented May 25, 2018

clip.exe can only copy but not paste.

A limitation that is most unfortunate. And unrelated to WSL. You can paste with right-click of course. But you made me curious enough (just) to do a search. [ed: Get-Clipboard. It's late 😴.]

$ powershell.exe Get-Clipboard
some text from the windows clipboard

Using backticks (`powershell.exe Get-Clipboard`) would execute whatever is in the Windows clipboard.

@laktak
Copy link

laktak commented May 25, 2018

xclip on Linux is great because you can pipe to/from it. WSL could support something similar for the Windows clipboard.

powershell.exe GetClipboard gives me GetClipboard : The term 'GetClipboard' is not recognized as the name of a cmdlet, ...

powershell.exe -Command Get-Clipboard (see above) does work but takes forever/a few seconds to complete.

@therealkenc
Copy link
Collaborator

WSL could support something similar

WSL could not; because the Windows clipboard is not in WSL.

That said the win32 team could add a paste feature to clip.exe. Or improve the startup time of powershell.exe. Or someone motivated could write a dedicated win32 program to do it. True enough that. But that's a general Windows ask independent of WSL. Windows could do a lot of things (faster).

takes forever/a few seconds to complete.

time powershell.exe Get-Clipboard
some text from the windows clipboard

real    0m0.402s
user    0m0.000s
sys     0m0.047s

Faster than typing, anyway 😉.

@HiImJulien
Copy link

@laktak I tried it. Apparently my WSL was broken, after I changed the my home directory to C:Kirsch\wsl_home\ . Works like a charm after reinstall; symlinked C:\repositories\ into ~\Repositories\ instead. Changing the home directory, however, breaks it.

I am just curious. Why is clip.exe not symbolically linked to clip?
Just a really small, minor change, but makes everything more intuitiv.

@therealkenc
Copy link
Collaborator

Why is clip.exe not symbolically linked to clip?

That is #2003. clip.exe is not special.

@HiImJulien
Copy link

Before I take this issue off-topic: 'clip.exe' is special, since it is basically a gateway between the WSL and the Host (unless I can install 'xclip' or 'pbcopy' inside WSL and still have it work).

@therealkenc
Copy link
Collaborator

No, it is a win32 program that calls win32 api functions. Just like every other .exe that does the same. cmd.exe is also a gateway between WSL and the host. notepad.exe is a gateway between WSL and the host. Any win32 program is a gateway to the host. Because... they run on the host. The ask is not well formed because: (a) WSL does not control userspace (notwithstanding our all singing all dancing /init). WSL is a kernel ABI. (b) Assuming /mnt/c or even /usr/bin exists violates separation of concerns. Which is why in 20 years no VM ships like that. (c) [mostly] It isn't the least bit counter-intuitive. It would be counter-intuitive if it behaved different, which means I have to answer why clip.exe is a symlink but <insert poster's favorite programs> isn't, until forever. And, (d) [failing (c)] You'd make it an alias not a symlink anyway; and no one stops you from doing that if that's how you like to roll.

@roachsinai
Copy link

@therealkenc Thanks for you solution, but each time I use clip.exe, could you tell me why I got an extra blank line like below

➜  ~ which pwdc
pwdc: aliased to pwd | clip.exe
➜  ~ pwdc
➜  ~ powershell.exe Get-Clipboard
/home/roach

➜  ~  

@therealkenc
Copy link
Collaborator

Not sure what version you are running, but I was seeing borkage like that with copy/paste in general for a period around the Fall Update. I noticed it whenever I copy/pasted from my shell into github posts in my browser; nothing to do with WSL. Problem was not specific to powershell.exe Get-Clipboard (which I never actually use IRL). There was also a -- appearing IIRC, along with the extra newline.

It seemed to have cleared up somewhere along the way, because my pastes have been fine "lately". I just tried your repro on 18305 and it seems to work correctly without the extra newline. I couldn't tell you what versions were/are affected exactly, and I don't have a 17763 (RS5) handy atm to test. You might check over in the console tracker and see if they know anything about it.

@roachsinai
Copy link

Thanks for your reply! I'm running version -- 17763.195,
image

But I installed the Insider Preview build in the Release Preview Ring. It seems I'm too slow. Are you in Fast Ring?

@therealkenc
Copy link
Collaborator

Don't know if anything has hit Preview Ring yet. 17763 is just bog standard 1809 aka RS5 aka 2018 Fall Update. Yes I'm on Fast Ring. I wouldn't sweat the Fast Ring unless this is something critical to your workflow.

@roachsinai
Copy link

OK, thanks again.

@fpigeonjr
Copy link

Hey Guys!
Do you know how to use this clip.exe tool with tmux?
Thanks!

clip.exe < ~/.ssh/id_rsa.pub worked for me to copy my ssh key to Github from WSL2.

@gosukiwi
Copy link

Adding to this, you can just alias xcopy="clip.exe" or alias pbcopy="clip.exe" if you are from macOS-world. And then you can do as you normally would:

$ cat ~/.ssh/id_rsa.pub | pbcopy

Works for simple use cases which is all I use it for :)

@dwmkerr
Copy link

dwmkerr commented Jul 27, 2020

Here's what I've been doing: (note this includes fixes suggested below!)

alias pbcopy='clip.exe'
alias pbpaste="powershell.exe -command 'Get-Clipboard' | tr -d '\r' | head -n -1"

The first one just aliases pbcopy to use the clip.exe executable. The second relies on Powershell to access the clipboard, then head to strip the superfluous carriage return/newline (thanks @walbertus).

Hope that's helpful. This is from the following: https://effective-shell.com/docs/part-1-transitioning-to-the-shell/4-clipboard-gymnastics/

Would love to see a "more native" solution like /dev/clipboard. I couldn't get xclip to work with any of the suggestions or variations here: https://askubuntu.com/questions/1035903/how-can-i-get-around-using-xclip-in-the-linux-subsystem-on-win-10

@walbertus
Copy link

@dwmkerr solution for pbpaste still gives superfluous newline for me. Using this one instead for the pbpaste.
I'm using ubuntu 20.04 with WSL2

alias pbpaste="powershell.exe -command 'Get-Clipboard' | head -n -1"

@dwmkerr
Copy link

dwmkerr commented Aug 2, 2020

Oh nice @walbertus that's much cleaner, updated my earlier comment!

@adrianmusante
Copy link

I tried to use the alias pbpaste="powershell.exe -command 'Get-Clipboard' | head -n -1" solution but it doesn't work well when the text is copied from Windows/GUI. It only works if the copy is made from the terminal with clip.exe.

I think that real problem is the new-line definition between Windows and Linux. Windows adds return-carry (\r) then when text is paste in terminal contain \r\n instead of only \n.

I changed head -n -1 for tr -d '\r' and got it to work the same way no matter if the text was copied by console or from GUI. This change remove all \r characters from text.

alias pbcopy="clip.exe"
alias pbpaste="powershell.exe -command 'Get-Clipboard' | tr -d '\r'"

@mickdekkers
Copy link

@adrianmusante the paste command still seems to add an extra trailing newline:

$ printf 'foo\nbar\n'
foo
bar

$ printf 'foo\nbar\n' | xxd
00000000: 666f 6f0a 6261 720a                      foo.bar.
$ printf 'foo\nbar\n' | pbcopy
$ pbpaste
foo
bar


$ pbpaste | xxd
00000000: 666f 6f0a 6261 720a 0a                   foo.bar..

You can tell the source of the issue is the paste command and not the copy command by pasting into your editor.

It looks like we still need head; piping the result into head -n -1 fixes it:

alias pbcopy='clip.exe'
alias pbpaste="powershell.exe -command 'Get-Clipboard' | tr -d '\r' | head -n -1"
$ printf 'foo\nbar\n'
foo
bar

$ printf 'foo\nbar\n' | xxd
00000000: 666f 6f0a 6261 720a                      foo.bar.
$ printf 'foo\nbar\n' | pbcopy
$ pbpaste
foo
bar

$ pbpaste | xxd
00000000: 666f 6f0a 6261 720a                      foo.bar.

@dwmkerr
Copy link

dwmkerr commented Aug 26, 2020

Awesome, updated my original comment!

@pt1997
Copy link

pt1997 commented Aug 27, 2020

I solved this for me by adding this in my .tmux.conf

#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 | sed 's/\r//g')\"; tmux paste-buffer"

@azinsharaf
Copy link

azinsharaf commented Oct 5, 2022

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.

Can't say for sure about the other X implementations, though, but I think redirecting the X clipboard to the windows clipboard is the natural thing to do for any X windows implementation.

this worked for me as well. OS: WSL 1 (Ubuntu)
using KPCLI application that uses xclip to copy username/password to the clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests