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


当前回答

此答案包含特定于macOS用户的详细信息。

将以下行追加到~/.vimrc:

set clipboard=unnamed

如果这不起作用,检查您安装的版本是否可能没有启用剪贴板功能。在编写这个答案时(2019年),macOS附带的默认vim没有启用剪贴板选项。您需要该选项来访问系统剪贴板。

要检查您的vim是否启用了该选项,请使用以下命令

vim --version | grep clipboard

结果,你应该有+剪贴板。如果是-clipboard,那么你的VIM没有访问系统剪贴板的选项。

您需要使用所需的选项制作并安装VIM。下面是命令。

# Create the directories you need
$ sudo mkdir -p /opt/local/bin
# Download, compile, and install the latest Vim
$ cd ~
$ git clone https://github.com/vim/vim.git
$ cd vim
$ ./configure --prefix=/opt/local
$ make
$ sudo make install
# Add the binary to your path, ahead of /usr/bin
$ echo 'PATH=/opt/local/bin:$PATH' >> ~/.bash_profile
# Reload bash_profile so the changes take effect in this window
$ source ~/.bash_profile"

上面的操作将安装启用选项+剪贴板的最新VIM。

现在您可以将文本拉到系统剪贴板。下面的步骤解释如何拉。

在vim命令模式下按v,这将切换到VISUAL模式。 移动光标以选择需要复制的文本或行。 按y,这将复制选定的文本到剪贴板。 转到任何外部应用程序,CMD + v进行粘贴。

我使用macOS Mojave的MACBook Pro,上面的工作都在里面。

其他回答

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

对于Mac OS X,在终端中:

运行vim——version | grep clipboard检查是否启用了剪贴板(使用+)

将下面的代码添加到.vimrc文件中

如果有(“剪贴板”) Set clipboard= named "复制到系统剪贴板 If has("unnamedplus") "X11支持 设置剪贴板+ = unnamedplus endif endif

重启终端和vim

参考:在Mac OS X上设置Vim到剪贴板

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

也许有人会觉得有用。我想要独立于X剪贴板,并且仍然能够在两个运行的vims之间复制和粘贴一些文本。这段小代码将所选文本保存在temp.txt文件中以供复制。将下面的代码放入您的.vimrc中。使用CTRL-c CTRL-v来完成这项工作。

cp /dev/null ~/temp.txt && cat > ~/temp.txt

Noremap:r !cat ~/temp.txt