3

I am using neovim on archlinux/WSL2, and try to communicate with the Windows clipboard in neovim.

Following the instructions in :h clipboard-wsl, I filled in init.lua with code:

vim.g.clipboard = {
 name = 'WslClipboard',
 copy = {
 ["+"] = 'clip.exe',
 ["*"] = 'clip.exe',
 },
 paste = {
 ["+"] = 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
 ["*"] = 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
},
 cache_enabled = 0,
 }

But when I try to yank using "+y, I get "Clipboard: No Provider". However running :checkhealth, I get:

Clipboard (optional)
   - OK Clipboard tool found: WslClipboard

If I run echo "hello world" | clip.exe in the terminal, all work fine: I can paste "hello world" in windows side. And so is the powershell.exe ... command.

After I yank something, I can't find the content in '+' register or '*' register, they are just missing in the result of :reg. So I'm inclined to think the clipboard function is not trigerred, then I tried to install xclip, and luckily the problem is solved.

7
  • Are you using Vim or Neovim? Which is the distribution you are using? Commented May 30, 2023 at 6:08
  • I can't reproduce your problem. It seems to work fine for me. Are the clip.exe and powershell.exe command running smooth in WSL? Commented May 30, 2023 at 11:38
  • @VivianDeSmedt I'm using neovim on archlinux@WSL2, and the clip.exe and powershell.exe both work fine in the terminal. It would be nice if you can tell me how to debug and what to look into.
    – Jerry Z
    Commented Jun 1, 2023 at 8:24
  • Something weird is going on. "Clipboard: No Provider" and "Clipboard tool found" are total contradictions. I'm inclined to think you backed into a corner case (a bug that appears rarely). Try reinstalling neovim. Try a newer version; try an older version. Try another wsl distribution. Basically try to reproduce this issue in multiple environments, and don't do any copy-pasting. This is to ensure that the bug is reproducible and isn't contingent on some of your config (that you haven't posted here because it's seemingly irrelevant).
    – 3N4N
    Commented Jun 1, 2023 at 9:49
  • 1
    @VivianDeSmedt It's {'paste': {'*': 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))', '+': 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))'}, 'cache_enabled': 0, 'name': 'WslClipboard', 'copy': {'*': 'clip.exe', '+': 'clip.exe'}}
    – Jerry Z
    Commented Jun 1, 2023 at 13:23

3 Answers 3

1

Well I fix the problem by simply installing xclip. I don't know why, and I used to think that is unneeded because I'm not going to use the clipboard in X11 but the one in Windows.

3
  • Did you also changed the neovim configuration? Commented Jun 1, 2023 at 14:35
  • I have xclip but because I'm in console mode xclip complains about Error: Can't open display: (null). Commented Jun 1, 2023 at 15:00
  • 1
    @VivianDeSmedt No, I didn't change any configuration.
    – Jerry Z
    Commented Jun 2, 2023 at 3:09
1

Try using win32yank its much faster than using powershell and xclip.

Download the win32yank binary here https://github.com/equalsraf/win32yank/releases then copy it to /usr/local/bin dont forget, should be chmod +x win32yank.exe.

After that add this to your neovim config.

    vim.g.clipboard = {
        name = "win32yank-wsl",
        copy = {
            ["+"] = "win32yank.exe -i --crlf",
            ["*"] = "win32yank.exe -i --crlf",
        },
        paste = {
            ["+"] = "win32yank.exe -o --lf",
            ["*"] = "win32yank.exe -o --lf",
        },
        cache_enabled = true,
    }
0

Run the script from https://github.com/victor-dev-00/clip-nvim :

vim.g.clipboard = {
    name = "win32yank-wsl",
    copy = {
        ["+"] = "win32yank.exe -i --crlf",
        ["*"] = "win32yank.exe -i --crlf",
    },
    paste = {
        ["+"] = "win32yank.exe -o --lf",
        ["*"] = "win32yank.exe -o --lf",
    },
    cache_enabled = true,
}
2
  • 2
    Welcome to Vim. Thanks for your contribution. Maybe could you insert the script to the answer and explain how it solves the problem. It would make the answer more self consistent :-) Commented Apr 3 at 5:19
  • Hi, I happily inserted the most essential part of the post, but honestly your answer would look really, really much better if you would also explain, what is this script doing and how.
    – peterh
    Commented Apr 14 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.