它是可能的管道到/从剪贴板在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
当前回答
Linux中有不同的剪贴板;X服务器有一个,窗口管理器可能有另一个,等等。没有标准的设备。
哦,是的,在CLI中,屏幕程序也有自己的剪贴板,就像Emacs和vi等其他一些应用程序一样。
在X中,您可以使用xclip。
你可以检查这个线程的其他可能的答案: http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-07/0919.html
其他回答
一种从剪贴板粘贴到文件的方法,不需要任何工具,除了echo。
转义你想要粘贴的文本中的单引号:用“\”替换所有出现的“\”并将结果复制到剪贴板。 输入echo -n ' 按Shift +插入 输入' > filename.txt 按回车键
基本上你是这样做的:
Echo -n复制“text”带“\”单引号“\”转义“> filename.txt”
即使复制的文本有新的行,它也能工作。
在Wayland上,xcopy似乎不能工作。使用wl-clipboard代替。
例如,在Fedora上:
sudo dnf install wl-clipboard
tree | wl-copy
wl-paste > file
昨天我发现自己遇到了一个问题:“如何在不同的用户会话之间共享剪贴板?”当使用Ctrl + Alt + F7 - Ctrl + Alt + F8切换会话时,实际上,您无法粘贴复制的内容。
我提出了以下快速和肮脏的解决方案,基于一个命名管道。它确实很简陋,但我发现它很实用:
user1@host:~$ mkfifo /tmp/sharedClip
然后在发送终端
user1@host:~$ cat > /tmp/sharedClip
最后,在接收端:
user2@host:~$ cat /tmp/sharedClip
现在,你在第一个终端中输入或粘贴任何东西,并且(在点击返回后),它将立即出现在接收终端中,从那里你可以复制和粘贴你喜欢的任何地方。
当然,这并不是严格地从user1的剪贴板中获取内容,使其在user2的剪贴板中可用,而是需要额外的粘贴和复制单击。
下面是一个可以在多个平台上使用的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
安装
# 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