它是可能的管道到/从剪贴板在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
当前回答
有几种方法。上面提到的一些方法包括(我认为)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`
绑定一对关键组合,你应该是好的。
其他回答
下面是一个可以在多个平台上使用的Bash脚本,用于读取剪贴板。
如果您要添加功能(例如,更多的平台),请在这里编辑脚本。
#!/bin/bash
# WF 2013-10-04
#
# Multi-platform clipboard read access
#
# Supports
# Mac OS X
# Git shell / Cygwin (Windows)
# Linux (e.g., Ubuntu)
#
# Display an error
#
error() {
echo "error: $1" 1>&2
exit 1
}
#
# getClipboard
#
function getClipboard() {
os=`uname`
case $os in
# Git Bash (Windows)
MINGW32_NT-6.1)
cat /dev/clipboard;;
# Mac OS X
Darwin*)
pbpaste;;
# Linux
Linux*)
# Works only for the X clipboard - a check that X is running might be due
xclip -o;;
*)
error "unsupported os $os";;
esac
}
tmp=/tmp/clipboard$$
getClipboard >$tmp
cat $tmp
# Comment out for debugging
rm $tmp
昨天我发现自己遇到了一个问题:“如何在不同的用户会话之间共享剪贴板?”当使用Ctrl + Alt + F7 - Ctrl + Alt + F8切换会话时,实际上,您无法粘贴复制的内容。
我提出了以下快速和肮脏的解决方案,基于一个命名管道。它确实很简陋,但我发现它很实用:
user1@host:~$ mkfifo /tmp/sharedClip
然后在发送终端
user1@host:~$ cat > /tmp/sharedClip
最后,在接收端:
user2@host:~$ cat /tmp/sharedClip
现在,你在第一个终端中输入或粘贴任何东西,并且(在点击返回后),它将立即出现在接收终端中,从那里你可以复制和粘贴你喜欢的任何地方。
当然,这并不是严格地从user1的剪贴板中获取内容,使其在user2的剪贴板中可用,而是需要额外的粘贴和复制单击。
在Windows上(使用Cygwin)尝试 Cat /dev/clipboard或echo "foo" > /dev/clipboard如本文所述。
安装
# You can install xclip using `apt-get`
apt-get install xclip
# or `pacman`
pacman -S xclip
# or `dnf`
dnf install xclip
如果你没有apt-get、pacman或dnf,源代码可以在sourceforge上找到。
设置
Bash
在~ /。bash_aliases,添加:
alias setclip="xclip -selection c"
alias getclip="xclip -selection c -o"
不要忘记使用。~ /。Bash_aliases或重新启动配置文件。
Fish
在~ / . config /鱼/配置。鱼,添加:
abbr setclip "xclip -selection c"
abbr getclip "xclip -selection c -o"
不要忘记通过重新启动终端来重新启动fish实例,以便应用更改。
使用
你现在可以使用setclip和getclip,例如:
$ echo foo | setclip
$ getclip
foo
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"
当我运行缩进,剪贴板里的东西都会缩进。