它是可能的管道到/从剪贴板在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 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。
其他回答
xsel在Debian/Ubuntu/Mint
# append to clipboard:
cat 'the file with content' | xsel -ab
# or type in the happy face :) and ...
echo 'the happy face :) and content' | xsel -ib
# show clipboard
xsel -ob
# Get more info:
man xsel
安装
sudo apt-get install xsel
在Wayland上,xcopy似乎不能工作。使用wl-clipboard代替。
例如,在Fedora上:
sudo dnf install wl-clipboard
tree | wl-copy
wl-paste > file
复制和粘贴到剪贴板在Windows (Cygwin):
See:
$ clip.exe -?
CLIP
Description:
Redirects output of command line tools to the Windows clipboard.
This text output can then be pasted into other programs.
Parameter List:
/? Displays this help message.
Examples:
DIR | CLIP Places a copy of the current directory
listing into the Windows clipboard.
CLIP < README.TXT Places a copy of the text from readme.txt
on to the Windows clipboard.
还有getclip(它可以代替Shift + Ins!)和putclip (echo oeuoa | putclip.exe将其放入clip)存在。
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"
当我运行缩进,剪贴板里的东西都会缩进。
在Windows子系统for Linux (WSL)上,您可以使用clip.exe复制到剪贴板:
cat file | clip.exe
请记住使用|管道命令。而不是>命令,因为这将不起作用。