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


当前回答

如果你的vim编译时没有+xterm_clipboard选项,就像Debian和Ubuntu中默认的那样,你可以将选择或整个缓冲区输送到处理桌面剪贴板的外部程序。对于xclip(您可能需要在前面安装它),命令将是:w !xclip -sel clip

其他回答

在vimrc文件中,可以指定自动使用系统剪贴板进行复制和粘贴。

macOS和Windows设置:

set clipboard=unnamed

Linux set (vim 7.3.74+):

set clipboard=unnamedplus

注意:您可能需要使用最新版本的Vim才能工作。

http://vim.wikia.com/wiki/Accessing_the_system_clipboard

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

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

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

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

在ubuntu 21.04, vim 8.2上测试。

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

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

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

vmap <C-c> "+y

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

Shift + Ctrl + C 如果你在Linux的图形模式,但首先你需要选择你需要复制什么。