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


当前回答

我已经使用这些很多年了:

nnoremap Y "+y
vnoremap Y "+y
nnoremap yY ^"+y$

你现在可以只使用大写Y复制到剪贴板,小写Y不会受到影响,例如通过set clipboard= named,所以你仍然可以选择拷贝是否会到剪贴板。

在ubuntu 21.04, vim 8.2上测试。

其他回答

对于OSX,就像上面的10342个答案所表明的那样,你需要确保vim支持剪贴板功能,并且OSX预发布的那个不支持剪贴板,如果你运行 酿造安装vim它将工作。

除了运行vi仍然会让你运行预先发布的OSX版本,而不是你从brew安装的版本。

为了解决这个问题,我简单地将我的vim命令别名为酿造版本,而不是OSX的默认版本:

alias vim="/usr/local/Cellar/vim/8.0.1100_1/bin/vim"

现在我是金的

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

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

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

vmap <C-c> "+y

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

在我的情况下,使用鼠标和右键复制的帮助进行选择。

我不希望包括行号,所以我:在复制之前设置nonnumber。

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

这里有一个你在浏览器中打开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>

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