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


当前回答

我是一个Vim newby,但是,复制所有的文本到系统剪贴板(例如,如果你想把它粘贴到文字处理器或其他文本编辑器,如gedit,鼠标垫等…),在正常模式:

ggVGy

或者,更简单地说:

:%y

按照建议,我还安装了vim-gtk和put

设置剪贴板= unnamedplus

在我的。vimrc

一切都很正常

如果你只想复制文本的一部分,使用可视模式(v),选择你想要复制的文本,然后按y。

最后,我建议使用剪贴板程序,如Clipman(我最喜欢的)、Clipit、Parcellite或类似的程序。

(我在Debian Stretch Xfce中使用vim 8.0)

原谅我的英语!:-)

其他回答

我不能复制到剪贴板的系统中,因为我的~/中有这个。vimrc文件:

 if has('mouse')
   set mouse=a
 endif

但如果你接下来添加这一行,它将允许你简单地按Ctrl+c将你所选择的任何东西放入剪贴板。

vmap <C-c> "+y

原始讨论和更多详细信息:启用set mouse=a从vim中复制文本

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

我想补充一种方法来复制一行到剪贴板,并在函数上使用它。

这里有一个你在浏览器中打开URL的例子

let g:chrome_exe = 'C:/.../Google/Chrome/Application/chrome.exe'
function OpenURL()
    " copy the line and put it in the register *
    normal "*yy
    :execute "silent !start ".g:chrome_exe." ".getreg("*")
endfunction

map ,url :call OpenURL()<CR>

在linux gnome (ubuntu, xubuntu等)安装vim-gnome,你将能够使用VISUAL选择,然后正常按Ctrl + C,用Ctrl + V粘贴到其他应用程序

sudo apt install vim-gnome

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)