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

cat file | clipboard

当前回答

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

Error: Can't open display: (null)

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

echo hello | clip.exe

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

其他回答

基于之前的文章,我最终得到了以下轻量级的别名解决方案,可以添加到.bashrc:

if [ -n "$(type -P xclip)" ]
then
  alias xclip='xclip -selection clipboard'
  alias clipboard='if [ -p /dev/stdin ]; then xclip -in; fi; xclip -out'
fi

例子:

# Copy
$ date | clipboard
Sat Dec 29 14:12:57 PST 2018

# Paste
$ date
Sat Dec 29 14:12:57 PST 2018

# Chain
$ date | clipboard | wc
   1       6      29

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

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

对于这个场景,我们可以使用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文件中调用它

在OS X上,使用pbcopy;Pbpaste的方向相反。

pbcopy < .ssh/id_rsa.pub

我写了这个小脚本,它把猜测工作从复制/粘贴命令中去掉。

该脚本的Linux版本依赖于系统中已经安装的xclip。这个脚本叫做剪贴板。

#!/bin/bash
# Linux version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard     # Pipe someapp's output into clipboard
#        clipboard | someapp     # Pipe clipboard's content into someapp
#

if command -v xclip 1>/dev/null; then
    if [[ -p /dev/stdin ]] ; then
        # stdin is a pipe
        # stdin -> clipboard
        xclip -i -selection clipboard
    else
        # stdin is not a pipe
        # clipboard -> stdout
        xclip -o -selection clipboard
    fi
else
    echo "Remember to install xclip"
fi

该脚本的OS X版本依赖于pbcopy和pbpaste,它们已预安装在所有mac电脑上。

#!/bin/bash
# OS X version
# Use this script to pipe in/out of the clipboard
#
# Usage: someapp | clipboard     # Pipe someapp's output into clipboard
#        clipboard | someapp     # Pipe clipboard's content into someapp
#

if [[ -p /dev/stdin ]] ; then
    # stdin is a pipe
    # stdin -> clipboard
    pbcopy
else
    # stdin is not a pipe
    # clipboard -> stdout
    pbpaste
fi

使用脚本非常简单,因为您只需像这两个示例中所示的那样通过管道输入或输出剪贴板。

$ cat file | clipboard

$ clipboard | less