是否可以直接从Vim复制到剪贴板?yy只把东西复制到Vim的内部缓冲区。我想复制到操作系统的剪贴板。在Vim中有这样的命令吗?或者你只能在Vim中提取东西?
当前回答
在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)都没有预先安装在你的发行版上,你可以在回购中找到它们
其他回答
@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.
我不能复制到剪贴板的系统中,因为我的~/中有这个。vimrc文件:
if has('mouse')
set mouse=a
endif
但如果你接下来添加这一行,它将允许你简单地按Ctrl+c将你所选择的任何东西放入剪贴板。
vmap <C-c> "+y
原始讨论和更多详细信息:启用set mouse=a从vim中复制文本
这个问题已经有很多答案了。我在添加我的方法,我觉得很快。
快速地,你可以按V (Shift + V)到激活可视模式。在可见模式下,可以使用j和k选择要复制的文本。选择后使用
"*y
现在,选定的文本被复制到剪贴板。
在我的实例中,用鼠标高亮显示(单击和拖动)选择文本会导致vim进入可视模式。
在mac上,最简单的解决方案是使用fn +鼠标点击和拖动,以避免进入可视模式。
如果您希望在默认情况下避免这种行为,可以编辑vimrc
除了vim-gnome, Ubuntu 20.04的neovim默认也支持+y。
如果你不想安装一个新程序,你可以使用cat file.txt或gedit file.txt的惰性方法从那里复制。