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


当前回答

仅在ubuntu终端下的vim中,

在vim中按shift +鼠标拖动选择文本,然后在终端上按CTRL + shift + c

然后在其他编辑器上按CTRL + v

其他回答

在Mac OSX上

复制所选部分:可视选择文本(正常输入v或v) 模式)和类型:w !pbcopy 复制整个文件:%w !pbcopy 从剪贴板粘贴:r !pbpaste

在大多数Linux发行版上,你可以替换:

用xclip -i -sel c或xsel -i -b拷贝上面的内容 使用xclip -o -sel -c或xsel -o -b进行Pbpaste 注意:如果这些工具(xsel和xclip)都没有预先安装在你的发行版上,你可以在回购中找到它们

我有问题,因为我的vim不支持剪贴板:

vim --version | grep clip
-clipboard       +insert_expand   +path_extra      +user_commands
+emacs_tags      -mouseshape      +startuptime     -xterm_clipboard

我安装了vim-gnome(支持剪贴板),然后再次检查:

vim --version | grep clipboard
+clipboard       +insert_expand   +path_extra      +user_commands
+emacs_tags      +mouseshape      +startuptime     +xterm_clipboard

现在我可以分别使用“+y”和“+p”进行复制和粘贴。

@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

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