在Bash中是否可以将内容导入/导出到剪贴板?
无论是将内容导入/导出到设备句柄,还是使用辅助应用程序,我都没有找到任何相关资料。
例如,如果/dev/clip
是指向剪贴板的设备链接,我们可以这样做:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
这个函数将测试剪贴板的存在并使用它。
为了验证,先将内容复制粘贴到你的Shell中,然后调用clippaste
函数:
clippaste () {
if [[ $OSTYPE == darwin* ]]
then
pbpaste
elif [[ $OSTYPE == cygwin* ]]
then
cat /dev/clipboard
else
if command -v xclip &> /dev/null
then
xclip -out -selection clipboard
elif command -v xsel
then
xsel --clipboard --output
else
print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
return 1
fi
fi
}
如果您正在使用“Windows终端”连接到Linux主机,则可以使用以下小脚本将文件复制到Windows主机的剪贴板:
# Clipfile
# - Sends a file into the Windows Terminal clipboard
printf $'\e]52;c;%s\a' "$(base64 ${1:?})"
我不确定这是否适用于其他终端和控制台程序?
感谢@MrCalvin在此回答中提供的解决方案:https://superuser.com/questions/1705770/copy-data-from-linux-console-to-windowshost-clipboard-using-windows-terminal?newreg=8ad2f39ec3f54a659b0c9e295540bbf1
:%y+
命令来复制所有行到 X 的 PRIMARY 剪贴板中,其中 X 代表的是 '+' 寄存器。如果您想要具体指定某些行,请将%
替换为一个范围。但是,这里有三个注意事项:1. 现在,您必须先将文本保存到文件中才能复制它。这与答案中提到的xclip
命令相反。2. 如果您不熟悉 vim,这可能会很繁琐。3. 只有在编译 vim 时启用了特定功能才能执行此操作。如果您安装了 GVim,则默认情况下应该在 vim 的 GUI 和终端实例中启用该功能。 - Braden Bestxclip
。你的回答非常好用。 - HankCa:%y+
这个方法,但没关系,你已经覆盖了所有情况!干得好。 - HankCals
输出的简单解决方案是:ls | xclip -i -selection clipboard
。 - user2585716