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

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

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

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

当前回答

在Windows子系统for Linux (WSL)上,您可以使用clip.exe复制到剪贴板:

cat file | clip.exe

请记住使用|管道命令。而不是>命令,因为这将不起作用。

其他回答

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

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"

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

pbcopy内置在OS X中:

复制.bash_profile文件的内容:

cat ~/.bash_profile | pbcopy

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

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

基本上你是这样做的:

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

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

这个函数将测试存在哪些剪贴板并使用它。

要验证复制粘贴到你的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
}