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

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

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

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

当前回答

复制和粘贴到剪贴板在Windows (Cygwin):

See:

$ clip.exe -?

CLIP
Description:
    Redirects output of command line tools to the Windows clipboard.
    This text output can then be pasted into other programs.
Parameter List:
/?                  Displays this help message.
Examples:
DIR | CLIP          Places a copy of the current directory
                        listing into the Windows clipboard.
CLIP < README.TXT   Places a copy of the text from readme.txt
                        on to the Windows clipboard.

还有getclip(它可以代替Shift + Ins!)和putclip (echo oeuoa | putclip.exe将其放入clip)存在。

其他回答

在macOS系统下,请使用内置的pbcopy和pbpaste命令。

例如,如果你跑步

cat ~/.bashrc | pbcopy

~/的内容。可以使用Cmd + V快捷方式粘贴bashrc文件。

要保存当前剪贴板到一个文件,将输出pbpaste重定向到一个文件:

pbpaste > my_clipboard.txt

确保使用别名xclip="xclip -selection c" 否则你将无法使用Ctrl+v进行粘贴。

例子: 执行echo -n test | xclip命令后,Ctrl+v将粘贴test

你可以使用大量的剪贴板。我想你可能是一个Linux用户,想把东西放到X Windows主剪贴板中。通常,您想要与之对话的剪贴板有一个实用程序可以让您与之对话。

在X的情况下,有xclip(和其他)。xclip -selection c将数据发送到剪贴板,在大多数应用程序中使用Ctrl + c, Ctrl + V。

如果你在Mac OS X上,有pbcopy。例如,cat example.txt | pbcopy

如果你在Linux终端模式下(没有X),那么查看有剪贴板的gpm或Screen。试试Screen命令readreg。

在Windows 10+或Cygwin下,使用/dev/clipboard或clip。

Try

xclip

xclip - command line interface to X selections (clipboard) 

man

有几种方法。上面提到的一些方法包括(我认为)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`

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