3

I just started using neovim and I am having issues trying to copy codes from neovim to system clipboard. I wanted to share some line of code and I am unable to copy the code into the system clipbooard.

I have tried lots of solutions. I have zero knowledge on lua. I copied a config file i liked and that is what I am using so i don't know what I am doing. Some of the things I have tried is installing win32yank,

vim.cmd [[
    let 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': 0,
      \ }
]]

I have also tried


let 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,
    \ }

Like i said ealier, I don't know what i am doing. I need help please.

2

2 Answers 2

5

I guess you are using lua but the problem is that both the snippets you shared are in vim script.

First of all, check if yank/paste is setup to use system clipboard or not. Add this to your config to enable yanking/pasting on system clipboard directly:

  • If you are using init.lua, then add vim.opt.clipboard = "unnamedplus".
  • Incase of init.vim, add set clipboard+=unnamedplus.

WSL

Since we are on WSL, there are couple of things we need to take care before setting up clipboard:

  1. Remove xclip from your system if already installed. It is known to cause problems with WSL.
  2. We also need to take care of line endings (CRLF->LF) when pasting on Neovim. we can use sed to remove all occurences of /r(CR) at the end of the line.

Now we have two options for copy-pasting across Linux and Windows:

WSLg (Fast)

Here, we rely on WSLg to sync Linux (Wayland) system clipboard with Windows one. Make sure wl-clipboard is installed on your system. and WSLg is enabled. You can check if wslg is enabled by running following command and checking if WSLg version is shown or not:

$ wsl.exe --version
WSL version: 1.2.5.0
Kernel version: 5.15.90.1
WSLg version: 1.0.51                   
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.1778

Finally, add following lines to your init.lua:

if vim.fn.has("wsl") == 1 then
    if vim.fn.executable("wl-copy") == 0 then
        print("wl-clipboard not found, clipboard integration won't work")
    else
        vim.g.clipboard = {
            name = "wl-clipboard (wsl)",
            copy = {
                ["+"] = 'wl-copy --foreground --type text/plain',
                ["*"] = 'wl-copy --foreground --primary --type text/plain',
            },
            paste = {
                ["+"] = (function()
                    return vim.fn.systemlist('wl-paste --no-newline|sed -e "s/\r$//"', {''}, 1) -- '1' keeps empty lines
                end),
                ["*"] = (function() 
                    return vim.fn.systemlist('wl-paste --primary --no-newline|sed -e "s/\r$//"', {''}, 1)
                end),
            },
            cache_enabled = true
        }
    end
end

Calling Windows commands from WSL (slow)

This works regardless of whether WSLg is enabled or not.

  1. Download win32yank.exe from here or use following commands:
curl -sLo/tmp/win32yank.zip https://github.com/equalsraf/win32yank/releases/download/v0.1.1/win32yank-x64.zip
unzip -p /tmp/win32yank.zip win32yank.exe > /tmp/win32yank.exe
chmod +x /tmp/win32yank.exe
sudo mv /tmp/win32yank.exe /usr/local/bin/
  1. make sure /usr/local/bin is in $PATH.

  2. Add following to init.lua:

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,
}

Happy Vimming!

0

I am using Windows. So the code below might help. Here is what I use in my init.lua file. It map the yank operation for example [yy] to ["+yy],the ["+yy] means using ["+] register perform [yy] operation. ["+] register is the register that direct interact with system clipboard.

-- set system clipboard
vim.keymap.set('n','y','"+y')
vim.keymap.set('n','yy','"+yy')
vim.keymap.set('n','Y','"+Y')
vim.keymap.set('x','y','"+y')
vim.keymap.set('x','Y','"+Y')

Your Answer

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

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