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


当前回答

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)

其他回答

除了vim-gnome, Ubuntu 20.04的neovim默认也支持+y。

如果你不想安装一个新程序,你可以使用cat file.txt或gedit file.txt的惰性方法从那里复制。

如果你正在使用GVim,你也可以设置guioptions+=a。这将触发自动复制到剪贴板的文本,您在视觉模式突出显示。 缺点:注意,高级剪贴板管理器(历史)将在这种情况下得到你所有的选择历史…

在抄写到剪贴板上时,我很吃力。在Vim内部,使用可视模式非常简单,但如果你想复制到剪贴板,事情是相当混乱的。我有一个简单的复制方法使用xclip实用程序。为此,您必须首先安装xclip。

对于整个文件来说非常简单

Xclip -sel剪辑文件名

但如果您只想复制特定范围的行号 Tail -n +[n1] filename | head -n [n2] | xclip -sel剪辑

你可以使用~/。Bashrc来化简

#rangecp copy lines from n1 to n2 from a given file to clipboard
function rangecp()
{
    if [ -f $1 ]
    then
        if [ ! -z $3 ]
        then
            diff=$(($3 - $2 + 1))
            #diff=`expr $3 - $2 + 1`
            tail -n +$2  $1 | head -n $diff | xclip -sel clip
        elif [ ! -z $2 ]
        then
            tail -n +$2  $1 | xclip -sel clip
        else
            echo "Provide a range from [n1] lines to [n2] lines"
        fi
    else
        echo "[ $1 ] file doesn't exist"
    fi
}

then

源~ / . bashrc 如何使用

rangecp filename.txt 50 89
rangecp filename.txt 50

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