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

cat file | clipboard

当前回答

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

pbcopy < .ssh/id_rsa.pub

其他回答

把这个添加到~/.bashrc:

# Now `cclip' copies and `clipp' pastes'
alias cclip='xclip -selection clipboard'
alias clipp='xclip -selection clipboard -o'

现在剪贴粘贴和剪贴复制-但你也可以做更花哨的东西: 剪切| sed 's/^/ /' |剪辑 ↑缩进您的剪贴板;适用于没有堆栈溢出{}按钮的站点

您可以通过运行以下命令添加它:

printf "\nalias clipp=\'xclip -selection c -o\'\n" >> ~/.bashrc
printf "\nalias cclip=\'xclip -selection c -i\'\n" >> ~/.bashrc

一样:

your_command_which_gives_output | pbcopy

WSL / GNU/Linux(需要xclip包):

your_command_which_gives_output | xclip -sel clip

Git Bash在Windows:

your_command_which_gives_output | clip

基于之前的文章,我最终得到了以下轻量级的别名解决方案,可以添加到.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

对于mac,这是一个示例方法复制(到剪贴板)粘贴(从剪贴板)使用命令行

将pwd命令的结果复制到剪贴板as

$ pwd | pbcopy

使用剪贴板中的内容通过您的机器的快捷方式进行粘贴或在命令中,如下所示

$ cd $(pbpaste)

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

该脚本的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