是否可以直接从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
其他回答
Shift + Ctrl + C 如果你在Linux的图形模式,但首先你需要选择你需要复制什么。
使用寄存器“+”复制到系统剪贴板(即。+y而不是y)。
同样,您可以从“+”粘贴从系统剪贴板(即。"+p而不是p)。
我已经使用这些很多年了:
nnoremap Y "+y
vnoremap Y "+y
nnoremap yY ^"+y$
你现在可以只使用大写Y复制到剪贴板,小写Y不会受到影响,例如通过set clipboard= named,所以你仍然可以选择拷贝是否会到剪贴板。
在ubuntu 21.04, vim 8.2上测试。
此答案包含特定于macOS用户的详细信息。
将以下行追加到~/.vimrc:
set clipboard=unnamed
如果这不起作用,检查您安装的版本是否可能没有启用剪贴板功能。在编写这个答案时(2019年),macOS附带的默认vim没有启用剪贴板选项。您需要该选项来访问系统剪贴板。
要检查您的vim是否启用了该选项,请使用以下命令
vim --version | grep clipboard
结果,你应该有+剪贴板。如果是-clipboard,那么你的VIM没有访问系统剪贴板的选项。
您需要使用所需的选项制作并安装VIM。下面是命令。
# Create the directories you need
$ sudo mkdir -p /opt/local/bin
# Download, compile, and install the latest Vim
$ cd ~
$ git clone https://github.com/vim/vim.git
$ cd vim
$ ./configure --prefix=/opt/local
$ make
$ sudo make install
# Add the binary to your path, ahead of /usr/bin
$ echo 'PATH=/opt/local/bin:$PATH' >> ~/.bash_profile
# Reload bash_profile so the changes take effect in this window
$ source ~/.bash_profile"
上面的操作将安装启用选项+剪贴板的最新VIM。
现在您可以将文本拉到系统剪贴板。下面的步骤解释如何拉。
在vim命令模式下按v,这将切换到VISUAL模式。 移动光标以选择需要复制的文本或行。 按y,这将复制选定的文本到剪贴板。 转到任何外部应用程序,CMD + v进行粘贴。
我使用macOS Mojave的MACBook Pro,上面的工作都在里面。
@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.