pbcopy&pbpaste
简单介绍
pbcopy 命令将标准输入复制到剪贴板。然后,你可以在任何地方使用 pbpaste 命令粘贴剪贴板内容。
Mac系统下,把命令行的输出存到剪切板
pwd | pbcopy
输出剪切板中的内容
pbpaste
pbcopy可以将输出内容复制到剪贴板中,可以复制文本、命令执行结果或者是文件中的内容
echo "test pbcopy" | pbcopy
tree | pbcopy
cat text.txt | pbcopy
pbpaste则是将剪切板内容输出。
pbpaste
WARNING
在图形界面下使用鼠标右键或者是 Ctrl + C
复制的内容也会存储在 pbcopy 中。
在 Linux 上使用
由于 Linux 和 Mac OS 是基于 Unix 的系统,因此许多命令可以在两个平台上运行。但是,某些命令可能只在一个平台上有,比如 pbcopy 和 pbpast 就是 Mac 系统下的命令。
从 Mac OS 切换到 Linux 的发行版的人将会找不到这两个命令,Linux 中没有 pbcopy 和 pbpaste 命令。但是,我们可以通过 shell 别名使用 xclip 和/或 xsel 命令复制 pbcopy 和 pbpaste 命令的功能。xclip 和 xsel 包存在于大多数 Linux 发行版的默认存储库中。请注意,你无需安装这两个程序。只需安装上述任何一个程序即可。
要在 Arch Linux 及其衍生产版上安装它们,请运行:
sudo pacman xclip
在 Fedora 上:
sudo dnf xclip
在 Debian、Ubuntu、Linux Mint 上:
sudo apt install xclip
安装后,你需要为 pbcopy 和 pbpaste 命令创建别名。为此,请编辑 ~/.bashrc
:
vi ~/.bashrc
如果要使用 xclip,请粘贴以下行到 ~/.bashrc
文件尾部:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
如果要使用 xsel,请在 ~/.bashrc
中粘贴以下行;并且之前安装 xclip 的命令也要改为安装 xsel。
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
保存并关闭文件。
接下来,运行以下命令以更新 ~/.bashrc
中的更改。
source ~/.bashrc
ZSH 用户将上述行粘贴到 ~/.zshrc
中。
TIP
至此,Linux 下的配置工作就已经完成了。之后可以像在 Mac 平台一样使用 pbcopy 和 pbpaste 命令了。
man page
File: *manpages*, Node: pbcopy, Up: (dir)
PBCOPY(1) General Commands Manual PBCOPY(1)
NAME
pbcopy, pbpaste - provide copying and pasting to the pasteboard (the
Clipboard) from command line
SYNOPSIS
pbcopy [-help] [-pboard {general | ruler | find | font}]
pbpaste [-help] [-pboard {general | ruler | find | font}] [-Prefer {txt
| rtf | ps}]
DESCRIPTION
pbcopy takes the standard input and places it in the specified
pasteboard. If no pasteboard is specified, the general pasteboard will
be used by default. The input is placed in the pasteboard as plain
text data unless it begins with the Encapsulated PostScript (EPS) file
header or the Rich Text Format (RTF) file header, in which case it is
placed in the pasteboard as one of those data types.
pbpaste removes the data from the pasteboard and writes it to the
standard output. It normally looks first for plain text data in the
pasteboard and writes that to the standard output; if no plain text data
is in the pasteboard it looks for Encapsulated PostScript; if no EPS is
present it looks for Rich Text. If none of those types is present in the
pasteboard, pbpaste produces no output.
* Encoding:
pbcopy and pbpaste use locale environment variables to determine the
encoding to be used for input and output. For example, absent other
locale settings, setting the environment variable LANG=en_US. UTF-8 will
cause pbcopy and pbpaste to use UTF-8 for input and output. If an
encoding cannot be determined from the locale, the standard C encoding
will be used. Use of UTF-8 is recommended. Note that by default the
Terminal application uses the UTF-8 encoding and automatically sets the
appropriate locale environment variable.
OPTIONS
-pboard {general | ruler | find | font}
specifies which pasteboard to copy to or paste from. If no
pasteboard is given, the general pasteboard will be used by
default.
-Prefer {txt | rtf | ps}
tells pbpaste what type of data to look for in the pasteboard
first. As stated above, pbpaste normally looks first for plain
text data; however, by specifying -Prefer ps you can tell pbpaste
to look first for Encapsulated PostScript. If you specify -Prefer
rtf, pbpaste looks first for Rich Text format. In any case,
pbpaste looks for the other formats if the preferred one is not
found. The txt option replaces the deprecated ascii option, which
continues to function as before. Both indicate a preference for
plain text.
SEE ALSO
ADC Reference Library:
Cocoa > Interapplication Communication > Copying and Pasting
Carbon > Interapplication Communication > Pasteboard Manager Programming
Guide
Carbon > Interapplication Communication > Pasteboard Manager Reference
BUGS
There is no way to tell pbpaste to get only a specified data type.
Apple Computer, Inc. January 12, 2005 PBCOPY(1)