从neovim中复制到系统剪贴板

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

Copy into system clipboard from neovim

问题

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

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

  1. let g:clipboard = {
  2. \ 'name': 'win32yank-wsl',
  3. \ 'copy': {
  4. \ '+': 'win32yank.exe -i --crlf',
  5. \ '*': 'win32yank.exe -i --crlf',
  6. \ },
  7. \ 'paste': {
  8. \ '+': 'win32yank.exe -o --lf',
  9. \ '*': 'win32yank.exe -o --lf',
  10. \ },
  11. \ 'cache_enabled': 0,
  12. \ }
  13. ]]

我还尝试过

  1. let g:clipboard = {
  2. \ 'name': 'WslClipboard',
  3. \ 'copy': {
  4. \ '+': 'clip.exe',
  5. \ '*': 'clip.exe',
  6. \ },
  7. \ 'paste': {
  8. \ '+': 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
  9. \ '*': 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
  10. \ },
  11. \ 'cache_enabled': 0,
  12. \ }

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

英文:

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,

  1. vim.cmd [[
  2. let g:clipboard = {
  3. \ 'name': 'win32yank-wsl',
  4. \ 'copy': {
  5. \ '+': 'win32yank.exe -i --crlf',
  6. \ '*': 'win32yank.exe -i --crlf',
  7. \ },
  8. \ 'paste': {
  9. \ '+': 'win32yank.exe -o --lf',
  10. \ '*': 'win32yank.exe -o --lf',
  11. \ },
  12. \ 'cache_enabled': 0,
  13. \ }
  14. ]]

I have also tried

  1. let g:clipboard = {
  2. \ 'name': 'WslClipboard',
  3. \ 'copy': {
  4. \ '+': 'clip.exe',
  5. \ '*': 'clip.exe',
  6. \ },
  7. \ 'paste': {
  8. \ '+': 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
  9. \ '*': 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
  10. \ },
  11. \ 'cache_enabled': 0,
  12. \ }

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的版本:

  1. $ wsl.exe --version
  2. WSL version: 1.2.5.0
  3. Kernel version: 5.15.90.1
  4. WSLg version: 1.0.51
  5. MSRDC version: 1.2.3770
  6. Direct3D version: 1.608.2-61064218
  7. DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
  8. Windows version: 10.0.22621.1778

最后,将以下内容添加到你的init.lua中:

  1. if vim.fn.has("wsl") == 1 then
  2. if vim.fn.executable("wl-copy") == 0 then
  3. print("wl-clipboard not found, clipboard integration won't work")
  4. else
  5. vim.g.clipboard = {
  6. name = "wl-clipboard (wsl)",
  7. copy = {
  8. ["+"] = 'wl-copy --foreground --type text/plain',
  9. ["*"] = 'wl-copy --foreground --primary --type text/plain',
  10. },
  11. paste = {
  12. ["+"] = (function()
  13. return vim.fn.systemlist('wl-paste --no-newline|sed -e "s/\r$//"', {''}, 1) -- '1' keeps empty lines
  14. end),
  15. ["*"] = (function()
  16. return vim.fn.systemlist('wl-paste --primary --no-newline|sed -e "s/\r$//"', {''}, 1)
  17. end),
  18. },
  19. cache_enabled = true
  20. }
  21. end
  22. end

从WSL调用Windows命令(较慢)

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

  1. 这里下载win32yank.exe或使用以下命令:
  1. curl -sLo /tmp/win32yank.zip https://github.com/equalsraf/win32yank/releases/download/v0.1.1/win32yank-x64.zip
  2. unzip -p /tmp/win32yank.zip win32yank.exe > /tmp/win32yank.exe
  3. chmod +x /tmp/win32yank.exe
  4. sudo mv /tmp/win32yank.exe /usr/local/bin/
  1. 确保/usr/local/bin$PATH中。

  2. 将以下内容添加到init.lua中:

  1. vim.g.clipboard = {
  2. name = 'win32yank-wsl',
  3. copy = {
  4. ['+'] = 'win32yank.exe -i --crlf',
  5. ['*'] = 'win32yank.exe -i --crlf',
  6. },
  7. paste = {
  8. ['+'] = 'win32yank.exe -o --lf',
  9. ['*'] = 'win32yank.exe -o --lf',
  10. },
  11. cache_enabled = true,
  12. }

愉快的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:

  1. $ wsl.exe --version
  2. WSL version: 1.2.5.0
  3. Kernel version: 5.15.90.1
  4. WSLg version: 1.0.51
  5. MSRDC version: 1.2.3770
  6. Direct3D version: 1.608.2-61064218
  7. DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
  8. Windows version: 10.0.22621.1778

Finally, add following lines to your init.lua:

  1. if vim.fn.has("wsl") == 1 then
  2. if vim.fn.executable("wl-copy") == 0 then
  3. print("wl-clipboard not found, clipboard integration won't work")
  4. else
  5. vim.g.clipboard = {
  6. name = "wl-clipboard (wsl)",
  7. copy = {
  8. ["+"] = 'wl-copy --foreground --type text/plain',
  9. ["*"] = 'wl-copy --foreground --primary --type text/plain',
  10. },
  11. paste = {
  12. ["+"] = (function()
  13. return vim.fn.systemlist('wl-paste --no-newline|sed -e "s/\r$//"', {''}, 1) -- '1' keeps empty lines
  14. end),
  15. ["*"] = (function()
  16. return vim.fn.systemlist('wl-paste --primary --no-newline|sed -e "s/\r$//"', {''}, 1)
  17. end),
  18. },
  19. cache_enabled = true
  20. }
  21. end
  22. 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:
  1. curl -sLo/tmp/win32yank.zip https://github.com/equalsraf/win32yank/releases/download/v0.1.1/win32yank-x64.zip
  2. unzip -p /tmp/win32yank.zip win32yank.exe > /tmp/win32yank.exe
  3. chmod +x /tmp/win32yank.exe
  4. sudo mv /tmp/win32yank.exe /usr/local/bin/
  1. make sure /usr/local/bin is in $PATH.

  2. Add following to init.lua:

  1. vim.g.clipboard = {
  2. name = 'win32yank-wsl',
  3. copy = {
  4. ['+'] = 'win32yank.exe -i --crlf',
  5. ['*'] = 'win32yank.exe -i --crlf',
  6. },
  7. paste = {
  8. ['+'] = 'win32yank.exe -o --lf',
  9. ['*'] = 'win32yank.exe -o --lf',
  10. },
  11. cache_enabled = true,
  12. }

Happy Vimming!

答案2

得分: 0

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

  1. -- 设置系统剪贴板
  2. vim.keymap.set('n', 'y', '"+y')
  3. vim.keymap.set('n', 'yy', '"+yy')
  4. vim.keymap.set('n', 'Y', '"+Y')
  5. vim.keymap.set('x', 'y', '"+y')
  6. 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.

  1. -- set system clipboard
  2. vim.keymap.set('n','y','"+y')
  3. vim.keymap.set('n','yy','"+yy')
  4. vim.keymap.set('n','Y','"+Y')
  5. vim.keymap.set('x','y','"+y')
  6. 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:

确定