是否可以直接从Vim复制到剪贴板?yy只把东西复制到Vim的内部缓冲区。我想复制到操作系统的剪贴板。在Vim中有这样的命令吗?或者你只能在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
其他回答
我的解决方案是把下面的行放在。vimrc:
map <C-y> :w !xclip -sel c <CR><CR>
脚本使用Ctrl + y将所选的行(通过可视模式)或文件内容(如果没有被选择)复制到剪贴板。如果这很重要,我使用的是Manjaro Linux。
在抄写到剪贴板上时,我很吃力。在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-gnome, Ubuntu 20.04的neovim默认也支持+y。
如果你不想安装一个新程序,你可以使用cat file.txt或gedit file.txt的惰性方法从那里复制。
在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).