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

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

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

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

当前回答

如果您像我一样在没有根权限的Linux服务器上运行,并且没有任何xclip或GPM,您可以通过使用临时文件来解决这个问题。例如:

$ echo "Hello, World!" > ~/clip
$ echo `cat ~/clip`
Hello, World!

其他回答

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

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

Mac版:

echo "Hello World" | pbcopy
pbpaste

它们位于/usr/bin/pbcopy和/usr/bin/pbpaste。

在Windows上(使用Cygwin)尝试 Cat /dev/clipboard或echo "foo" > /dev/clipboard如本文所述。

下面是一个可以在多个平台上使用的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

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

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

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