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

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

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

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

当前回答

一种从剪贴板粘贴到文件的方法,不需要任何工具,除了echo。

转义你想要粘贴的文本中的单引号:用“\”替换所有出现的“\”并将结果复制到剪贴板。 输入echo -n ' 按Shift +插入 输入' > filename.txt 按回车键

基本上你是这样做的:

Echo -n复制“text”带“\”单引号“\”转义“> filename.txt”

即使复制的文本有新的行,它也能工作。

其他回答

昨天我发现自己遇到了一个问题:“如何在不同的用户会话之间共享剪贴板?”当使用Ctrl + Alt + F7 - Ctrl + Alt + F8切换会话时,实际上,您无法粘贴复制的内容。

我提出了以下快速和肮脏的解决方案,基于一个命名管道。它确实很简陋,但我发现它很实用:

user1@host:~$ mkfifo /tmp/sharedClip

然后在发送终端

user1@host:~$ cat > /tmp/sharedClip

最后,在接收端:

user2@host:~$ cat /tmp/sharedClip

现在,你在第一个终端中输入或粘贴任何东西,并且(在点击返回后),它将立即出现在接收终端中,从那里你可以复制和粘贴你喜欢的任何地方。

当然,这并不是严格地从user1的剪贴板中获取内容,使其在user2的剪贴板中可用,而是需要额外的粘贴和复制单击。

这是一个简单的Python脚本,可以满足你的需要:

#!/usr/bin/python

import sys

# Clipboard storage
clipboard_file = '/tmp/clipboard.tmp'

if(sys.stdin.isatty()): # Should write clipboard contents out to stdout
    with open(clipboard_file, 'r') as c:
        sys.stdout.write(c.read())
elif(sys.stdout.isatty()): # Should save stdin to clipboard
    with open(clipboard_file, 'w') as c:
        c.write(sys.stdin.read())

将其保存为路径中的可执行文件(我将其保存到/usr/local/bin/clip.)你可以输入要保存到剪贴板上的内容。

echo "Hello World" | clip

你可以将剪贴板中的内容传输到其他程序……

clip | cowsay
 _____________
< Hello World >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

单独运行它只会输出剪贴板中的内容。

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"

当我运行缩进,剪贴板里的东西都会缩进。

安装xcopy实用程序,当你在终端中,输入:

Copy

Thing_you_want_to_copy | xclip -selection c

粘贴

myvariable=$(xclip -selection clipboard -o)

我注意到很多答案推荐pbpaste和pbcopy。如果您喜欢这些实用程序,但由于某种原因它们在存储库中不可用,那么您总是可以为xcopy命令创建别名,并将它们称为pbpaste和pbcopy。

alias pbcopy="xclip -selection c"
alias pbpaste="xclip -selection clipboard -o"

所以它看起来是这样的:

Thing_you_want_to_copy | pbcopy
myvariable=$(pbpaste)

一个名为doug的用户在我的评论中给出了一个答案。既然我发现它很有帮助,我决定在回答中重申一遍。

复制和粘贴到剪贴板在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)存在。