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:
- Remove
xclip
from your system if already installed. It is known to cause problems with WSL.
- 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.
- 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/
make sure /usr/local/bin
is in $PATH
.
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!
set clipboard=unnamedplus
? - As described in: github.com/neovim/neovim/wiki/…vim.opt.clipboard = 'unnamed'
in lua