从neovim中复制到系统剪贴板

huangapple go评论63阅读模式
英文:

Copy into system clipboard from neovim

问题

我刚开始使用 neovim,但在尝试将代码从 neovim 复制到系统剪贴板时遇到了问题。我想分享一些代码行,但无法将代码复制到系统剪贴板。

我尝试了很多解决方案。我对 Lua 一无所知。我复制了一个我喜欢的配置文件,所以我不知道我在做什么。我尝试过安装 win32yank,

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

我还尝试过

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

正如我之前所说,我不知道我在做什么。我需要帮助,请。

英文:

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.

答案1

得分: 5

我猜你正在使用Lua,但问题是你分享的这两个代码片段都是Vim脚本。

首先,检查复制/粘贴是否设置为使用系统剪贴板。将以下内容添加到你的配置中以启用在系统剪贴板上复制/粘贴:

  • 如果你使用init.lua,那么添加vim.opt.clipboard = "unnamedplus"
  • 如果你使用init.vim,添加set clipboard+=unnamedplus

WSL

由于我们在WSL上操作,设置剪贴板之前需要注意一些事项:

  1. 如果已经安装了xclip,请将其从系统中移除。它已知会在WSL上引发问题。
  2. 当在Neovim中粘贴时,我们还需要注意行尾的换行符(CRLF->LF),可以使用sed来移除所有行尾的/r(CR)出现。

现在我们有两个选项用于在Linux和Windows之间进行复制和粘贴:

WSLg (快速)

在这里,我们依赖于WSLg来同步Linux(Wayland)系统剪贴板与Windows系统剪贴板。
确保你的系统上安装了wl-clipboard,并且已启用WSLg。你可以通过运行以下命令来检查是否启用了wslg,并检查是否显示了WSLg的版本:

$ 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

最后,将以下内容添加到你的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

从WSL调用Windows命令(较慢)

这个方法不受WSLg是否启用的影响。

  1. 这里下载win32yank.exe或使用以下命令:
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. 确保/usr/local/bin$PATH中。

  2. 将以下内容添加到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,
}

愉快的Vim操作!

英文:

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!

答案2

得分: 0

我正在使用Windows。所以下面的代码可能会有所帮助。
以下是我在我的init.lua文件中使用的内容。
它将yank操作映射为例如[yy]到["+yy],其中["+yy]表示使用["+]寄存器执行[yy]操作。["+]寄存器是直接与系统剪贴板交互的寄存器。

-- 设置系统剪贴板
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')
英文:

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')

huangapple
  • 本文由 发表于 2023年2月24日 01:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定