如何将命令的输出直接复制到剪贴板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5130968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:25:05  来源:igfitidea点击:

How can I copy the output of a command directly into my clipboard?

linuxshellunixterminalclipboard

提问by Legend

How can I pipe the output of a command into my clipboard and paste it back when using a terminal? For instance:

使用终端时,如何将命令的输出通过管道传输到剪贴板并将其粘贴回?例如:

cat file | clipboard

采纳答案by Legend

I always wanted to do this and found a nice and easy way of doing it. I wrote down the complete procedure just in case anyone else needs it.

我一直想这样做,并找到了一种很好且简单的方法。我写下了完整的程序,以防其他人需要它。

First install a 16 kB program called xclip:

首先安装一个 16 kB 的程序,名为xclip

sudo apt-get install xclip

You can then pipe the output into xclipto be copied into the clipboard:

然后,您可以通过管道将输出xclip复制到剪贴板中:

cat file | xclip

To paste the text you just copied, you shall use:

要粘贴刚刚复制的文本,您应使用:

xclip -o

To simplify life, you can set up an alias in your .bashrc file as I did:

为了简化生活,您可以像我一样在 .bashrc 文件中设置别名:

alias "c=xclip"
alias "v=xclip -o"

To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+Ton some systems, but this is just for illustration purposes):

为了看看这有多大用处,假设我想在一个新的终端窗口中打开我当前的路径(在某些系统上可能还有其他方法,例如Ctrl+ T,但这仅用于说明目的):

Terminal 1:
pwd | c

Terminal 2:
cd `v`

Notice the ` `around v. This executes vas a command first and then substitutes it in-place for cdto use.

注意` `周围的v. 这v首先作为命令执行,然后就地替换它以供cd使用。

Only copy the content to the Xclipboard

只将内容复制到X剪贴板

cat file | xclip

If you want to paste somewhere else other than a Xapplication, try this one:

如果您想粘贴X应用程序以外的其他地方,请尝试以下操作:

cat file | xclip -selection clipboard

回答by DavidPhillipOster

On OS X, use pbcopy; pbpastegoes in the opposite direction.

在 OS X 上,使用pbcopy; pbpaste往相反的方向走。

回答by Bob Enohp

I wrote this little script that takes the guess work out of the copy/paste commands.

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

The Linux version of the script relies on xclip being already installed in your system. The script is called clipboard.

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

The OS X version of the script relies on pbcopy and pbpaste which are preinstalled on all Macs.

该脚本的 OS X 版本依赖于预装在所有 Mac 上的 pbcopy 和 pbpaste。

#!/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

Using the script is very simple since you simply pipe in or out of clipboardas shown in these two examples.

使用该脚本非常简单,因为您只需clipboard按照这两个示例中的管道输入或输出即可。

$ cat file | clipboard

$ clipboard | less

回答by Dirk Duschinger

I made a small tool providing similar functionality, without using xclip or xsel. stdoutis copied to a clipboard and can be pasted again in the terminal. See:

我制作了一个提供类似功能的小工具,不使用 xclip 或 xsel。stdout复制到剪贴板并可以再次粘贴到终端中。看:

https://sourceforge.net/projects/commandlinecopypaste/

https://sourceforge.net/projects/commandlinecopypaste/

Note, that this tool does not need an X-session. The clipboard can just be used within the terminal and has not to be pasted by Ctrl+Vor middle-mouse-click into other X-windows.

请注意,此工具不需要 X 会话。剪贴板只能在终端中使用,而不必通过Ctrl+V或鼠标中键粘贴到其他 X-windows 中。

回答by d.raev

Without using external tools, if you are connecting to the server view SSH, this is a relatively easy command:

不使用外部工具,如果是连接服务器查看SSH,这是一个比较简单的命令:

From a Windows 7+ command prompt:

从 Windows 7+ 命令提示符:

ssh user@server cat /etc/passwd | clip

This will put the content of the remote file to your local clipboard.

这会将远程文件的内容放入本地剪贴板。

(The command requires running Pageant for the key, or it will ask you for a password.)

(该命令需要为密钥运行 Pageant,否则它会要求您输入密码。)

回答by user3276552

Add this to to your ~/.bashrc:

将此添加到您的~/.bashrc

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

Now clipppastes and cclip copies — but you can also do fancier stuff:

clipp | sed 's/^/    /' | cclip

↑ indents your clipboard; good for sites without stack overflow's { }button

现在剪辑p粘贴和c剪辑副本——但你也可以做更有趣的事情:

clipp | sed 's/^/    /' | cclip

↑ 缩进剪贴板;适用于没有堆栈溢出{ }按钮的网站

You can add it by running this:

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

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

回答by tristobal

In Linux with xclip installed:

在安装了 xclip 的 Linux 中:

xclip -selection clipboard < file

xclip -selection 剪贴板 < 文件

回答by Michal Przybylowicz

I am using Parcellite and xselto copy last commit message from gitto my clipboard manager (for some reason xclipdoes not work):

我正在使用 Parcellitexsel并将最后一次提交消息复制git到我的剪贴板管理器(由于某种原因xclip不起作用):

$ git log -1 --pretty=%B | xsel -i -b

回答by Bruno Bronosky

I've created a tool for Linux/OSX/Cygwin that is similar to some of these others but slightly unique. I call it cband it can be found in this github gist.

我为 Linux/OSX/Cygwin 创建了一个工具,它与其他一些工具类似,但略有独特。我称之为它cb它可以在这个 github gist 中找到

In that gist I demonstrate how to do copy and paste via commandline using Linux, macOS, and Cygwin.

在那个要点中,我演示了如何使用 Linux、macOS 和 Cygwin 通过命令行进行复制和粘贴。

Linux

Linux

_copy(){
    cat | xclip -selection clipboard
}

_paste(){
    xclip -selection clipboard -o
}

macOS

苹果系统

_copy(){
    cat | pbcopy
}

_paste(){
    pbpaste
}

Cygwin

赛格温

_copy(){
    cat > /dev/clipboard
}

_paste(){
    cat /dev/clipboard
}

Note: I originally just intended to mention this in my comment to Bob Enohp's answer. But then I realized that I should add a README to my gist. Since the gist editor doesn't offer a Markdown preview I used the answer box here and after copy/pasting it to my gist thought, "I might as well submit the answer."

注意:我最初只是想在我对Bob Enohp 的回答的评论中提到这一点。但后来我意识到我应该在我的要点中添加一个自述文件。由于要点编辑器不提供 Markdown 预览,因此我在此处使用了答案框,然后将其复制/粘贴到我的要点中,“我不妨提交答案。”

cb

CB

A leak-proof tee to the clipboard

防漏三通到剪贴板

This script is modeled after tee(see man tee).

此脚本仿照tee(请参阅参考资料man tee)。

It's like your normal copy and paste commands, but unified and able to sense when you want it to be chainable

它就像你普通的复制和粘贴命令,但统一并且能够感知你希望它何时被链接

Examples

例子

Copy

复制

$ date | cb
# clipboard contains: Tue Jan 24 23:00:00 EST 2017

Paste

粘贴

# clipboard retained from the previous block
$ cb
Tue Jan 24 23:00:00 EST 2017
$ cb | cat
Tue Jan 24 23:00:00 EST 2017
$ cb > foo
$ cat foo
Tue Jan 24 23:00:00 EST 2017

Chaining

链接

$ date | cb | tee updates.log
Tue Jan 24 23:11:11 EST 2017
$ cat updates.log
Tue Jan 24 23:11:11 EST 2017
# clipboard contains: Tue Jan 24 23:11:11 EST 2017

Copy via file redirect

通过文件重定向复制

(chronologically it made sense to demo this at the end)

(按时间顺序,最后演示这个是有意义的)

# clipboard retained from the previous block
$ cb < foo
$ cb
Tue Jan 24 23:00:00 EST 2017
# note the minutes and seconds changed from 11 back to 00

回答by DFeng

For those using bash installed on their windows system (known as Windows Subsystem for Linux (WSL)), attempting xclip will give an error:

对于那些使用安装在其 Windows 系统上的 bash(称为 Linux 的 Windows 子系统(WSL)),尝试 xclip 将给出错误:

Error: Can't open display: (null)

Instead, recall that linux subsystem has access to windows executables. It's possible to use clip.exe like

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

echo hello | clip.exe

which allows you to use the paste command (ctrl-v).

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