Since the past few weeks, my muscle memory has really begun to hate me. I had switched all my systems over to Wayland because [X11 bad](https://xkcd.com/963/) and I had switched my CLI text editor from Vim to [Micro](https://micro-editor.github.io/) because constantly looking up Vim shortcuts makes me slower.
One of the things I can never remember how to do in Vim is copy and paste to the system clipboard. It's some permutation of `*"+y` right? Something like that. However, with Micro, you already know how to copy and paste (because it's literally just Ctrl+C Ctrl+V and your grandma could figure that out). Also, Vim doesn't support copying and pasting in Wayland by default unless you add some magic to your `~/.vimrc`, while Micro only requires you to install the (generally incredibly useful) [wl-clipboard](https://github.com/bugaevc/wl-clipboard) utility and it'll just work™.
## But...
Yeah, that just sounds too good to be true. Wayland has a nifty little tool called [Waypipe](https://gitlab.freedesktop.org/mstoeckl/waypipe) that's just like X11 forwarding plus [VirtualGL](https://virtualgl.org/) (by the way VirtualGL is one of those evil softwares that breaks regularly and laughs when you try to debug it). You get GPU-accelerated remote windows and it's an indispensible feature in my daily workflow. However, Micro copy paste behaves incredibly strangely in a Waypipe remote session.
First of all, when you try to copy, Waypipe spits out a [very annoying informational line](https://gitlab.freedesktop.org/mstoeckl/waypipe/-/issues/62) that screws over your nice TUI app, but you can easily comment it out in the Waypipe source and recompile.
However, there's a deeper issue: after copying, you can paste into your terminal emulator just fine, but trying to paste into anything else will just lead to pasting *nothing*. And after you close the Waypipe session, it goes back to pasting whatever was originally in your clipboard.
Even stranger, Micro claims to use wl-clipboard's `wl-copy` for copying to the clipboard, and running `wl-copy hello` in a Waypipe session works perfectly. Perfectly!
## Diving deeper
OK, that's pretty spooky, so let's take a look at the Micro source code. [internal/clipboard/clipboard.go](https://github.com/zyedidia/micro/blob/master/internal/clipboard/clipboard.go) is the relevant file, but oops, it just uses another library [zyedidia/clipboard](https://github.com/zyedidia/clipboard) for the hard lifting so let's read that library instead. [clipboard_unix.go](https://github.com/zyedidia/clipboard/blob/master/clipboard_unix.go) is the file we want in that library, and after a close inspection, we do run `wl-copy` with `os.exec`, but the text we want to copy is *piped* into `wl-copy`.
Yay, progress! Running `echo hello | wl-copy` behaves in the same spooky way as Micro, so we know the bug isn't in either Micro or zyedidia/clipboard.
So it certainly smells like the bug is in wl-clipboard, so I went ahead and filed [an issue](https://github.com/bugaevc/wl-clipboard/issues/141) there.
Here's what happened next:
### Me
When running `wl-copy` on a remote server that you've connected to using [Waypipe](https://gitlab.freedesktop.org/mstoeckl/waypipe), copying text from stdin does not work:
```bash
me@ThinkPad-X1-Yoga-Gen-6 ~> waypipe ssh exozyme # Connect to the remote machine with Waypipe
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
ta180m@exozyme ~> echo hello | wl-copy # Doesn't work
```
However, copying text if it's specified in the command does work:
```bash
me@ThinkPad-X1-Yoga-Gen-6 ~> waypipe ssh exozyme # Connect to the remote machine with Waypipe
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
ta180m@exozyme ~> wl-copy hello # Does work
```
In the first case, I'm not really sure what gets copied to the clipboard, but when I try pasting, nothing is pasted while Waypipe is still open. Then if I close Waypipe and the remote connection and try pasting, the thing originally in my clipboard is pasted. For instance:
```bash
me@ThinkPad-X1-Yoga-Gen-6 ~> wl-paste
A
me@ThinkPad-X1-Yoga-Gen-6 ~> waypipe ssh exozyme # Connect to the remote machine with Waypipe
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
ta180m@exozyme ~> wl-paste
A
ta180m@exozyme ~> echo B | wl-copy # Doesn't work
ta180m@exozyme ~> wl-paste # This only works in the current shell, if I open up a new one and try wl-paste, it pastes A
B
ta180m@exozyme ~> exit
Connection to exozy.me closed.
me@ThinkPad-X1-Yoga-Gen-6 ~> wl-paste
A
```
Any idea what's going on here?
### Maintainer
Hmm, not sure, I'm not familiar with how waypipe works. Maybe WAYLAND_DEBUG=1 on wl-copy could clear something up?
### Me
This is the output of `echo B | WAYLAND_DEBUG=1 wl-copy` in Waypipe:
```
[4030950.284] -> wl_display@1.get_registry(new id wl_registry@2)
[4030950.314] -> wl_display@1.sync(new id wl_callback@3)
In the first case, `wl-copy` seems to be copying control characters, which is why pasting doesn't result in anything being pasted.
### Maintainer
That looks rather strange, especially considering it's the MIME type that gets weird characters, not the contents...
### Me
Manually specifying the MIME type with `echo B | WAYLAND_DEBUG=1 wl-copy -t text/plain-text` works in Waypipe. How is the MIME type normally inferred?
### Me
I think I made more progress: wl-clipboard uses `xdg-mime query filetype` for inferring the MIME type right? If so, `xdg-mime query filetype somefile` works in a local shell but **does not output anything at all** in a Waypipe remote session.
## Aha!
So wl-clipboard is actually an innocent victim too, just like Micro and the clipboard library! `xdg-mime` is the real culprit here, so maybe time to file an issue for them?
Not so fast, let's take a glance at the `xdg-mime` source code. Here's the relevant lines:
`xdg-mime` thinks I'm using KDE 4 because this is a remote session with no environment variables set! Well, actually, I do have one variable set: `XDG_CURRENT_DESKTOP=KDE` in my `~/.ssh/environment` so apps run over Waypipe know to use KDE theming. `xdg-mime` thinks I'm an old-timer still on KDE 4 and uses the KDE 4 `kfile` command to detect the MIME type. But since I'm running hemorrhaging-edge KDE 5.soon-to-be-25.0 on Arch, that `kfile` command doesn't exist and `xdg-mime` interally errors and outputs nothing. Setting `KDE_SESSION_VERSION=5` in `~/.ssh/environment` makes copying text from stdin work in Waypipe! Case closed, 🎉
I'll end with a TL;DR, since I copied this verbatim from that wl-clipboard issue and I don't want to write an actual ending:
The funny thing is that the whole reason I went down this rabbit hole of chasing this bug was because copying text didn't work in Micro, so I read the Micro source code, then read the `zyedidia/clipboard` library code that Micro was using, then the `wl-clipboard` source code since that's what `zyedidia/clipboard` internally uses, then read the `xdg-mime` source code. (The magic of open source!) Turns out the bug was *much* deeper than I originally expected, and nothing's wrong with wl-clipboard or Waypipe. 😃