它是可能的管道到/从剪贴板在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
当前回答
安装
# 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
其他回答
安装
# 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
在Linux中,这是可行的:
cat filename | xclip
对于mac,你可以使用这个函数,它使用pbcopy和pbpaste,但更简单:
将此添加到你的.bashrc或.zshrc:
clp() {
if [[ -z "$1" ]]
then
# No input - act as paste
pbpaste;
else
# Input exists - act as copy
echo "$1" | pbcopy;
fi
}
复制使用clp“Content”,粘贴使用clp
我找到了一个很好的参考:如何瞄准多个选择与xclip
在我的情况下,我想粘贴内容在剪贴板上,也看到什么被粘贴在那里,所以我也使用tee命令与文件描述符:
echo "just a test" | tee >(xclip -i -selection clipboard)
>()是进程替换的一种形式。Bash用连接到括号内程序的标准输入的文件描述符的路径替换它们。
tecommand将您的命令分叉,允许您“管道其内容”并在标准输出“stdout”上查看结果。
你也可以创建别名来获取和写在剪贴板上,允许你使用“pbcopy”和“pbpaste”,就像你在Mac上一样。在我的情况下,当我使用Z shell (zsh)时,我在我的别名文件中有这个:
(( $+commands[xclip] )) && {
alias pbpaste='xclip -i -selection clipboard -o'
alias pbcopy='xclip -selection clipboard'
}
Z shell中的(($+命令[name]))测试命令“name”是否安装在您的系统上,然后两个别名都用{}分组。&&是一个二进制AND;如果a,那么b,因此如果你有xclip,那么别名将被设置。
echo "another test" | tee >(pbcopy)
要获取剪贴板内容,只需输入:
pbpaste | "any-command-you-need-here"
一种从剪贴板粘贴到文件的方法,不需要任何工具,除了echo。
转义你想要粘贴的文本中的单引号:用“\”替换所有出现的“\”并将结果复制到剪贴板。 输入echo -n ' 按Shift +插入 输入' > filename.txt 按回车键
基本上你是这样做的:
Echo -n复制“text”带“\”单引号“\”转义“> filename.txt”
即使复制的文本有新的行,它也能工作。