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


当前回答

@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

我是一个Vim newby,但是,复制所有的文本到系统剪贴板(例如,如果你想把它粘贴到文字处理器或其他文本编辑器,如gedit,鼠标垫等…),在正常模式:

ggVGy

或者,更简单地说:

:%y

按照建议,我还安装了vim-gtk和put

设置剪贴板= unnamedplus

在我的。vimrc

一切都很正常

如果你只想复制文本的一部分,使用可视模式(v),选择你想要复制的文本,然后按y。

最后,我建议使用剪贴板程序,如Clipman(我最喜欢的)、Clipit、Parcellite或类似的程序。

(我在Debian Stretch Xfce中使用vim 8.0)

原谅我的英语!:-)

对于Mac OS X,在终端中:

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

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

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

重启终端和vim

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

我在mac osx(10.15.3)和新的vim。我发现这太令人沮丧了,这里所有的答案都太复杂了,或者不适用于我的情况。我最终在两个方面得到了这个工作:

使用pbcopy的键映射:适用于mac附带的旧版本的vim。 将vmap ":w !pbcopy<CR><CR>添加到~/.vimrc中 现在您可以直观地选择并点击“(两个撇号)复制到剪贴板 安装新版本的vim,这样我就可以访问其他答案中最推荐的解决方案: 编译安装vim的别名vim=/usr/local/bin/vim(应该把这个添加到~/。Bashrc或同等语言) 现在您可以直观地选择并点击“+yy复制到剪贴板

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