是否可以直接从Vim复制到剪贴板?yy只把东西复制到Vim的内部缓冲区。我想复制到操作系统的剪贴板。在Vim中有这样的命令吗?或者你只能在Vim中提取东西?


当前回答

我的解决方案是把下面的行放在。vimrc:

map <C-y> :w !xclip -sel c <CR><CR>

脚本使用Ctrl + y将所选的行(通过可视模式)或文件内容(如果没有被选择)复制到剪贴板。如果这很重要,我使用的是Manjaro Linux。

其他回答

@Jacob Dalton has mentioned this in a comment, but nobody seems to have mentioned in an answer that vim has to be compiled with clipboard support for any of the suggestions mentioned here to work. Mine wasn't configured that way on Mac OS X by default and I had to rebuild vim. Use this the command to find out whether you have it or not vim --version | grep 'clipboard'. +clipboard means you're good and the suggestions here will work for you, while -clipboard means you have to recompile and rebuild vim.

如果你的vim编译时没有+xterm_clipboard选项,就像Debian和Ubuntu中默认的那样,你可以将选择或整个缓冲区输送到处理桌面剪贴板的外部程序。对于xclip(您可能需要在前面安装它),命令将是:w !xclip -sel clip

总结一下,让新手更容易上手,

复制当前行,在命令模式输入:

"*yy

要复制整个文件/缓冲区,在命令模式下,首先通过gg转到开头,然后键入

"*yG

如上所述,这需要vim——version中的+clipboard,这表示支持剪贴板,-clipboard表示不支持。

Shift + Ctrl + C 如果你在Linux的图形模式,但首先你需要选择你需要复制什么。

关于这个问题,我看到了很多答案,而解决这个问题的方法是多种答案的结合。

我遵循的步骤,使vim复制到系统剪贴板

使用sudo apt卸载vim。(我太懒了,不知道如何用+剪贴板支持重新编译它。 使用sudo apt Install带有+clipboard的vim-athena安装另一个名为vim-athena的vim包。 添加到~/。Vimrc下面的行:set clipboard=unnamedplus。 通过进入命令模式并输入Source %来查找文件。 保存并退出。

注意: 我使用的是Ubuntu 20.04。