我如何才能管道输出的命令到我的剪贴板,并粘贴回来时,使用终端?例如:

cat file | clipboard

当前回答

只是为了覆盖一个边缘情况:),因为问题标题问(至少现在)如何将命令的输出直接复制到剪贴板。

我经常发现,在命令已经执行并且我不想或不能再次执行之后,复制命令的输出是很有用的。

对于这个场景,我们可以使用gdm或类似的鼠标实用程序并使用鼠标进行选择。apt-get安装gdm,然后右键单击或Cntrl+Shift+c和Cntrl+Shift+v组合在终端进行复制和粘贴

Or, which is the preferred method for me (as the mouse cannot select properly inside one pane when you have multiple panes side by side and you need to select more than one line), using tmux we can copy into the tmux buffer using the standard [ , space , move to select , enter or you can select a block of code. Also this is particularly useful when you are inside one of the lanes of the cli multiplexer like tmux AND you need to select a bunch of text, but not the line numbers (my vim setup renders line numbers)

在此之后,您可以使用命令:

tmux save-buffer - | xclip -i

当然,您可以将其命名为您喜欢的名称,或者直接绑定到tmux配置文件中

这只是给您一个概念性的答案,以覆盖这种不可能再次执行命令的边缘情况。如果您需要更具体的代码示例,请告诉我

干杯

其他回答

有一个方法可以在不安装xclip/xsel的情况下工作,如果您没有sudo访问权限,这个方法非常有用。

你只需要安装vimx,这是VIM +剪贴板支持。你可以在大多数发行版中找到它。

将脚本保存为~/copy_to_clipboard.sh,

#!/bin/sh
if [ -z "$1" ]
  then
  str=" "
else
  str=$1
fi

vimx -u NONE -U NONE -N -c "let @a=\".\"" -c "let @*= \"$str\" " -c " put a " -c "sleep 2" -c " q!"

赋予执行权限:chmod +x ~/copy_to_clipboard 并调用它:

~/copy_to_clipboard.sh STRING

出于某种原因,您需要设置一个虚拟的@a寄存器,并将其粘贴到临时文件上。否则剪贴板寄存器将无法正确设置。 最后丢弃临时Vim文件。

你的剪贴板将被设置,你可以用鼠标中点击粘贴它

对于那些使用bash安装在他们的windows系统(称为windows子系统For Linux (WSL))上的用户,尝试xclip将会给出一个错误:

Error: Can't open display: (null)

相反,回想一下linux子系统可以访问windows可执行文件。可以像这样使用clip.exe

echo hello | clip.exe

它允许您使用粘贴命令(ctrl-v)。

我来自一个精简的KDE背景,没有访问xclip, xsel或其他花哨的东西。我有一个TCSH Konsole让事情变得更糟。

必备条件:qdbus klipper xargs bash

创建一个bash可执行文件foo.sh。

#!/bin/bash
qdbus org.kde.klipper /klipper setClipboardContents "$1" > /dev/null

注意:这需要bash,因为TCSH不支持多行参数。

后面跟着。cshrc. xml文件中的TCSH别名。

alias clipboard xargs -0 /path/to/foo

解释:

Xargs -0将stdin管道到单个参数中。该参数被传递给bash可执行文件,该可执行文件使用qdbus向klipper发送“复制到剪贴板”请求。通往/dev/null的管道是不将qdbus返回的换行符打印到控制台。

使用示例:

ls | clipboard

这将把当前文件夹的内容复制到剪贴板中。

注意:只适用于管道。如果需要复制参数,直接使用bash可执行文件。

只是为了覆盖一个边缘情况:),因为问题标题问(至少现在)如何将命令的输出直接复制到剪贴板。

我经常发现,在命令已经执行并且我不想或不能再次执行之后,复制命令的输出是很有用的。

对于这个场景,我们可以使用gdm或类似的鼠标实用程序并使用鼠标进行选择。apt-get安装gdm,然后右键单击或Cntrl+Shift+c和Cntrl+Shift+v组合在终端进行复制和粘贴

Or, which is the preferred method for me (as the mouse cannot select properly inside one pane when you have multiple panes side by side and you need to select more than one line), using tmux we can copy into the tmux buffer using the standard [ , space , move to select , enter or you can select a block of code. Also this is particularly useful when you are inside one of the lanes of the cli multiplexer like tmux AND you need to select a bunch of text, but not the line numbers (my vim setup renders line numbers)

在此之后,您可以使用命令:

tmux save-buffer - | xclip -i

当然,您可以将其命名为您喜欢的名称,或者直接绑定到tmux配置文件中

这只是给您一个概念性的答案,以覆盖这种不可能再次执行命令的边缘情况。如果您需要更具体的代码示例,请告诉我

干杯

这是Arch Linux用户的一个很好的解决方案。用pacman安装xsel,如下所示:

sudo pacman -S xsel

在~/中创建别名。Bashrc文件,比如:

alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'

用源代码重新加载终端:

source ~/.bashrc

像上面提到的那样使用它:

cat your_file.txt | pbcopy

供参考,在~/中填充所有别名是很好的做法。别名并在。bashrc文件中调用它