它是可能的管道到/从剪贴板在Bash?

无论是连接到设备手柄还是使用辅助应用程序,我都找不到任何东西。

例如,如果/dev/clip是一个连接到剪贴板的设备,我们可以这样做:

cat /dev/clip        # Dump the contents of the clipboard
cat foo > /dev/clip  # Dump the contents of "foo" into the clipboard

当前回答

安装xcopy实用程序,当你在终端中,输入:

Copy

Thing_you_want_to_copy | xclip -selection c

粘贴

myvariable=$(xclip -selection clipboard -o)

我注意到很多答案推荐pbpaste和pbcopy。如果您喜欢这些实用程序,但由于某种原因它们在存储库中不可用,那么您总是可以为xcopy命令创建别名,并将它们称为pbpaste和pbcopy。

alias pbcopy="xclip -selection c"
alias pbpaste="xclip -selection clipboard -o"

所以它看起来是这样的:

Thing_you_want_to_copy | pbcopy
myvariable=$(pbpaste)

一个名为doug的用户在我的评论中给出了一个答案。既然我发现它很有帮助,我决定在回答中重申一遍。

其他回答

Linux中有不同的剪贴板;X服务器有一个,窗口管理器可能有另一个,等等。没有标准的设备。

哦,是的,在CLI中,屏幕程序也有自己的剪贴板,就像Emacs和vi等其他一些应用程序一样。

在X中,您可以使用xclip。

你可以检查这个线程的其他可能的答案: http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-07/0919.html

安装xcopy实用程序,当你在终端中,输入:

Copy

Thing_you_want_to_copy | xclip -selection c

粘贴

myvariable=$(xclip -selection clipboard -o)

我注意到很多答案推荐pbpaste和pbcopy。如果您喜欢这些实用程序,但由于某种原因它们在存储库中不可用,那么您总是可以为xcopy命令创建别名,并将它们称为pbpaste和pbcopy。

alias pbcopy="xclip -selection c"
alias pbpaste="xclip -selection clipboard -o"

所以它看起来是这样的:

Thing_you_want_to_copy | pbcopy
myvariable=$(pbpaste)

一个名为doug的用户在我的评论中给出了一个答案。既然我发现它很有帮助,我决定在回答中重申一遍。

昨天我发现自己遇到了一个问题:“如何在不同的用户会话之间共享剪贴板?”当使用Ctrl + Alt + F7 - Ctrl + Alt + F8切换会话时,实际上,您无法粘贴复制的内容。

我提出了以下快速和肮脏的解决方案,基于一个命名管道。它确实很简陋,但我发现它很实用:

user1@host:~$ mkfifo /tmp/sharedClip

然后在发送终端

user1@host:~$ cat > /tmp/sharedClip

最后,在接收端:

user2@host:~$ cat /tmp/sharedClip

现在,你在第一个终端中输入或粘贴任何东西,并且(在点击返回后),它将立即出现在接收终端中,从那里你可以复制和粘贴你喜欢的任何地方。

当然,这并不是严格地从user1的剪贴板中获取内容,使其在user2的剪贴板中可用,而是需要额外的粘贴和复制单击。

有几种方法。上面提到的一些方法包括(我认为)tmux、Screen、Vim、Emacs和shell。我不知道Emacs或Screen,所以我将介绍其他三个。

Tmux

虽然不是X选项,但tmux有一个通过前缀-[(前缀默认为Ctrl + B)访问的复制模式。用于此模式的缓冲区是独立的,专属于tmux,这提供了许多可能性,并使其在适当的情况下比X选择更通用。

要退出此模式,请按Q;为了导航,使用你的Vim或Emacs绑定(默认= Vim), hjkl用于移动,v/ v/ C-v用于字符/行/块选择,等等。选择完成后,按Enter键复制并退出模式。

要从这个缓冲区中粘贴,请使用前缀-]。

壳牌

X11的任何安装似乎默认都附带两个程序:xclip和xsel(有点像它同时附带startx和xinit)。大多数其他答案都提到了xclip,我非常喜欢xsel,因为它很简洁,所以我将介绍xsel。

从xsel (1 x):

Input options \ -a, --append \ append standard input to the selection. Implies -i. -f, --follow \ append to selection as standard input grows. Implies -i. -i, --input \ read standard input into the selection. Output options \ -o, --output \ write the selection to standard output. Action options \ -c, --clear \ clear the selection. Overrides all input options. -d, --delete \ Request that the current selection be deleted. This not only clears the selection, but also requests to the program in which the selection resides that the selected contents be deleted. Overrides all input options. Selection options \ -p, --primary \ operate on the PRIMARY selection (default). -s, --secondary \ operate on the SECONDARY selection. -b, --clipboard \ operate on the CLIPBOARD selection.

这就是你需要知道的全部。p(或无)为PRIMARY, s为SECONDARY, b为CLIPBOARD, o为输出。

示例:假设我想从TTY复制foo的输出,并将其粘贴到一个错误报告的网页。要做到这一点,理想的做法是从TTY/X会话复制到/从TTY/X会话。那么问题就变成了如何从TTY访问剪贴板?

在这个例子中,我们假设显示X会话:1。

$ foo -v
Error: not a real TTY
details:
blah blah @ 0x0000000040abeaf4
blah blah @ 0x0000000040abeaf8
blah blah @ 0x0000000040abeafc
blah blah @ 0x0000000040abeb00
...
$ foo -v | DISPLAY=:1 xsel -b # copies it into clipboard of display :1

然后我可以按Ctrl + V它到形式一如往常。

现在假设支持站点上的某个人给了我一个命令来解决这个问题。它既复杂又冗长。

$ DISPLAY=:1 xsel -bo
sudo foo --update --clear-cache --source-list="http://foo-software.com/repository/foo/debian/ubuntu/xenial/164914519191464/sources.txt"
$ $(DISPLAY=:1 xsel -bo)
Password for braden:
UPDATING %%%%%%%%%%%%%%%%%%%%%%% 100.00%
Clearing cache...
Fetching sources...
Reticulating splines...
Watering trees...
Climbing mountains...
Looking advanced...
Done.
$ foo
Thank you for your order. A pizza should arrive at your house in the next 20 minutes. Your total is $6.99

订购披萨似乎是对命令行的高效使用。

...在移动。

Vim

如果用+剪贴板编译(这很重要!检查你的vim——版本),vim应该可以访问X PRIMARY和CLIPBOARD的选择。这两个选择可以分别从*和+寄存器访问,并且可以像其他寄存器一样在空闲时写入和读取。

例如:

:%y+    ; copy/yank (y) everything (%) into the CLIPBOARD selection (+)
"+p     ; select (") the CLIPBOARD selection (+) and paste/put it
ggVG"+y ; Alternative version of the first example

但是,如果您的Vim副本不直接支持对X选择的访问,这也不是世界末日。您可以只使用上一节中描述的xsel技术。

:r ! xsel -bo ; read  (r) from the stdout of (!) `xsel -bo`
:w ! xsel -b  ; write (w) to the stdin of    (!) `xsel -b`

绑定一对关键组合,你应该是好的。

在这个线程中,有一个选项不需要安装任何gclip/xclip/xsel第三方软件。

Perl脚本(因为通常总是安装Perl)

use Win32::Clipboard;
print Win32::Clipboard::GetText();