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


当前回答

也许有人会觉得有用。我想要独立于X剪贴板,并且仍然能够在两个运行的vims之间复制和粘贴一些文本。这段小代码将所选文本保存在temp.txt文件中以供复制。将下面的代码放入您的.vimrc中。使用CTRL-c CTRL-v来完成这项工作。

cp /dev/null ~/temp.txt && cat > ~/temp.txt

Noremap:r !cat ~/temp.txt

其他回答

如果你有xclip,一个简单的方法复制文本到剪贴板如下:

你想复制的Yank文本。(香草vim中的y命令) 输入:call system("xclip -selection clipboard", @")

:call system()执行终端命令。它有两个参数,第一个参数是命令,第二个参数是要向该命令输送什么。例如:echom系统("head -1", "Hello\nWorld")返回Hello(带一些填充)。Echom返回命令的输出,而call不返回。

xclip -selection剪贴板只是复制文本到系统剪贴板,而不是默认的X剪贴板,(通过鼠标中间按钮访问)。

@"返回最后一个被猛拉的文本。是默认寄存器,但您可以使用任何寄存器。要查看所有寄存器的内容,输入:registers。

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

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

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

注意: 我使用的是Ubuntu 20.04。

在我的实例中,用鼠标高亮显示(单击和拖动)选择文本会导致vim进入可视模式。

在mac上,最简单的解决方案是使用fn +鼠标点击和拖动,以避免进入可视模式。

如果您希望在默认情况下避免这种行为,可以编辑vimrc

In case you don't want to use any graphical interface for vim and you prefer to just stick with terminal emulator there may be a much simpler approach to this problem. Instead of using yank or anything like this, first take a look at documentation of terminal you use. I've been struggling with the same issue (trying to use +clipboard and xclip and so on) and in the end it turned out that in my terminal emulator it's enough to just press shift and select any text you want to copy. That's it. Quite simple and no need for messing with configuration. (I use urxvt by the way).

Ubuntu - 2018年7月

使用寄存器“+”复制到系统剪贴板(即。+y而不是y)。

同样,您可以从“+”粘贴从系统剪贴板(即。"+p而不是p)。

您还必须确保编译的vim支持剪贴板。试一试:

vim --version | grep .xterm_clipboard -o 

如果它是-xterm_clipboard(一个负号前缀),则不支持。

下面是一些使用支持剪贴板的vim工作版本进行交换的说明。

$ sudo apt-get purge vim 
$ sudo apt-get autoremove (removes any extraneous vim dependency packages from system) 
$ sudo apt-get install vim-gnome (or `sudo apt-get install vim-gtk3` for newer Ubuntu versions) 

再次检查vim——version | grep .xterm_clipboard -o,您可以确认剪贴板现在是可用的(即。+ xterm_clipboard)