是否可以直接从Vim复制到剪贴板?yy只把东西复制到Vim的内部缓冲区。我想复制到操作系统的剪贴板。在Vim中有这样的命令吗?或者你只能在Vim中提取东西?
当前回答
对于OSX,就像上面的10342个答案所表明的那样,你需要确保vim支持剪贴板功能,并且OSX预发布的那个不支持剪贴板,如果你运行 酿造安装vim它将工作。
除了运行vi仍然会让你运行预先发布的OSX版本,而不是你从brew安装的版本。
为了解决这个问题,我简单地将我的vim命令别名为酿造版本,而不是OSX的默认版本:
alias vim="/usr/local/Cellar/vim/8.0.1100_1/bin/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.
对于Mac OS X,在终端中:
运行vim——version | grep clipboard检查是否启用了剪贴板(使用+)
将下面的代码添加到.vimrc文件中
如果有(“剪贴板”) Set clipboard= named "复制到系统剪贴板 If has("unnamedplus") "X11支持 设置剪贴板+ = unnamedplus endif endif
重启终端和vim
参考:在Mac OS X上设置Vim到剪贴板
总结一下,让新手更容易上手,
复制当前行,在命令模式输入:
"*yy
要复制整个文件/缓冲区,在命令模式下,首先通过gg转到开头,然后键入
"*yG
如上所述,这需要vim——version中的+clipboard,这表示支持剪贴板,-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).
在抄写到剪贴板上时,我很吃力。在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