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


当前回答

我的解决方案是把下面的行放在。vimrc:

map <C-y> :w !xclip -sel c <CR><CR>

脚本使用Ctrl + y将所选的行(通过可视模式)或文件内容(如果没有被选择)复制到剪贴板。如果这很重要,我使用的是Manjaro Linux。

其他回答

如果你有xclip,一个简单的方法复制文本到剪贴板如下:

你想复制的Yank文本。(香草vim中的y命令) 输入:call system("xclip -selection clipboard", @")

:call system()执行终端命令。它有两个参数,第一个参数是命令,第二个参数是要向该命令输送什么。例如:echom系统("head -1", "Hello\nWorld")返回Hello(带一些填充)。Echom返回命令的输出,而call不返回。

xclip -selection剪贴板只是复制文本到系统剪贴板,而不是默认的X剪贴板,(通过鼠标中间按钮访问)。

@"返回最后一个被猛拉的文本。是默认寄存器,但您可以使用任何寄存器。要查看所有寄存器的内容,输入:registers。

以上这些在我的windows笔记本电脑上都不起作用。

Ctrl+C可以用于复制。但我需要 Shift+插入到粘贴!

(一个很好的理由,总是有一个笔记本电脑,可以随时插入,而不需要按第二个键)

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

在抄写到剪贴板上时,我很吃力。在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

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

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

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

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

现在我是金的