它是可能的管道到/从剪贴板在Bash?
无论是连接到设备手柄还是使用辅助应用程序,我都找不到任何东西。
例如,如果/dev/clip是一个连接到剪贴板的设备,我们可以这样做:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
它是可能的管道到/从剪贴板在Bash?
无论是连接到设备手柄还是使用辅助应用程序,我都找不到任何东西。
例如,如果/dev/clip是一个连接到剪贴板的设备,我们可以这样做:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
当前回答
我找到了一个很好的参考:如何瞄准多个选择与xclip
在我的情况下,我想粘贴内容在剪贴板上,也看到什么被粘贴在那里,所以我也使用tee命令与文件描述符:
echo "just a test" | tee >(xclip -i -selection clipboard)
>()是进程替换的一种形式。Bash用连接到括号内程序的标准输入的文件描述符的路径替换它们。
tecommand将您的命令分叉,允许您“管道其内容”并在标准输出“stdout”上查看结果。
你也可以创建别名来获取和写在剪贴板上,允许你使用“pbcopy”和“pbpaste”,就像你在Mac上一样。在我的情况下,当我使用Z shell (zsh)时,我在我的别名文件中有这个:
(( $+commands[xclip] )) && {
alias pbpaste='xclip -i -selection clipboard -o'
alias pbcopy='xclip -selection clipboard'
}
Z shell中的(($+命令[name]))测试命令“name”是否安装在您的系统上,然后两个别名都用{}分组。&&是一个二进制AND;如果a,那么b,因此如果你有xclip,那么别名将被设置。
echo "another test" | tee >(pbcopy)
要获取剪贴板内容,只需输入:
pbpaste | "any-command-you-need-here"
其他回答
确保使用别名xclip="xclip -selection c" 否则你将无法使用Ctrl+v进行粘贴。
例子: 执行echo -n test | xclip命令后,Ctrl+v将粘贴test
在Linux中,这是可行的:
cat filename | xclip
Ruby的联机程序启发我尝试使用Python。
假设我们想要一个命令,将剪贴板中的任何内容缩进四个空格。它非常适合在Stack Overflow上共享代码片段。
$ pbpaste | python -c "import sys
for line in sys.stdin:
print(f' {line}')" | pbcopy
这不是打字错误。Python需要换行符来执行for循环。我们希望在一次传递中更改行,以避免在内存中构建额外的数组。
如果你不介意构建额外的数组尝试:
$ pbpaste | python -c "import sys; print(''.join([f' {l}' for l in sys.stdin]))" | pbcopy
但老实说,awk在这方面比python更好。我在~/中定义了这个别名。bashrc文件(
alias indent="pbpaste | awk '{print \" \"\$0}' | pbcopy"
当我运行缩进,剪贴板里的东西都会缩进。
有几种方法。上面提到的一些方法包括(我认为)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`
绑定一对关键组合,你应该是好的。
你可以使用大量的剪贴板。我想你可能是一个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。