使用操作系统终端命令行的命令把输出复制到剪贴板 All In One
使用操作系统终端命令行的命令把输出复制到剪贴板 All In One
use command-line command to copy output to clipboard from the Terminal
Linux 系统 xclip
/ xsel
Debian / Ubuntu / Raspberry Pi OS
# Raspberry Pi
$ grep shutdown | ???
# xclip
$ sudo apt-get install xclip
$ sudo apt install xclip
# xsel, 没有才需要安装
$ sudo apt-get install xsel
$ sudo apt install xsel
https://www.raspberrypi.com/documentation/computers/os.html
macOS 系统
pbcopy
# zsh
$ grep shutdown | pbcopy
$ grep shutdown | pbcopy > shutdown.md
# pbpaste
$ pbpaste
xclip
for macOS
# macOS
$ brew install xclip
https://formulae.brew.sh/formula/xclip
https://ports.macports.org/port/xclip/
https://apps.apple.com/lc/app/xclip/id1623411951?mt=12
https://github.com/astrand/xclip/issues/52
https://www.jianshu.com/p/76f7317b75fd
Windows 系统 clip
# CMD / PowerShell
$ dir > clip
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
xclip
$ xclip --help
-bash: xclip:未找到命令
# xclip,没有才需要安装
$ sudo apt-get install xclip
# $ sudo apt install xclip
# 为 pbcopy 和 pbpaste 命令创建别名
$ vim ~/.bashrc
# $ code ~/.bashrc
# 给 xclip -selection 配置别名 ✅
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
# 刷新配置
$ source ~/.bashrc
apt-get
vs apt
???
$ man xclip
$ xclip -version
xclip version 0.13
Copyright (C) 2001-2008 Kim Saunders et al.
Distributed under the terms of the GNU GPL
display
bug ❌
Error: Can't open display: (null)
$ lsusb | xclip -o > xclip.md
# Error: Can't open display: (null)
$ lsusb | xclip -o >> xclip.md
# Error: Can't open display: (null)
$DISPLAY
# 设置 $DISPLAY 环境变量 ✅
$ export DISPLAY=:0.0
# 错误用法 ❌
$ lsusb | xclip -o >> xclip.md
Error: target STRING not available
# 正确的用法 ✅ (先复制,后粘贴)
# 使用多次 pipe 管道操作 🚀
$ lsusb | xclip -i | xclip -o >> xclip.md
# 1. 先复制输入 ✅
$ lsusb | xclip -i
# 2. 后粘贴输出 ✅
$ xclip -o >> xclip.md
xsel
# xsel, 没有才需要安装
$ sudo apt-get install xsel
$ sudo apt install xsel
# 为 pbcopy 和 pbpaste 命令创建别名 ✅
$ vim ~/.bashrc
$ code ~/.bashrc
# 给 xsel --clipboard 配置别名
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
# 刷新配置
$ source ~/.bashrc
$ man xsel
$ xsel --version
xsel version 1.2.0 by Conrad Parker <conrad@vergenet.net>
bug ❌
xsel: Can't open display: (null): Inappropriate ioctl for device
pi@raspberrypi:~/Desktop $ lsusb
Bus 001 Device 004: ID 2e8a:0005 MicroPython Board in FS mode
Bus 001 Device 003: ID 0424:ec00 Microchip Technology, Inc. (formerly SMSC) SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Microchip Technology, Inc. (formerly SMSC) SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@raspberrypi:~/Desktop $ lsusb | xsel
xsel: Can't open display: (null)
: Inappropriate ioctl for device
pi@raspberrypi:~/Desktop $ lsusb | xsel --clipboard
xsel: Can't open display: (null)
: Inappropriate ioctl for device
pi@raspberrypi:~/Desktop $ lsusb | xsel --input --clipboard
xsel: Can't open display: (null)
: Inappropriate ioctl for device
pi@raspberrypi:~/Desktop $ lsusb | xsel --input --clipboard >> xsel.md
xsel: Can't open display: (null)
: Inappropriate ioctl for device
$ xsel --help
$ xsel --help
$ xsel --help
Usage: xsel [options]
Manipulate the X selection.
By default the current selection is output and not modified if both
standard input and standard output are terminals (ttys). Otherwise,
the current selection is output if standard output is not a terminal
(tty), and the selection is set from standard input if standard input
is not a terminal (tty). If any input or output options are given then
the program behaves only in the requested mode.
If both input and output is required then the previous selection is
output before being replaced by the contents of standard input.
Input options
-a, --append Append standard input to the selection
-f, --follow Append to selection as standard input grows
-z, --zeroflush Overwrites selection when zero ('\0') is received
-i, --input Read standard input into the selection
Output options
-o, --output Write the selection to standard output
Action options
-c, --clear Clear the selection
-d, --delete Request that the selection be cleared and that
the application owning it delete its contents
Selection options
-p, --primary Operate on the PRIMARY selection (default)
-s, --secondary Operate on the SECONDARY selection
-b, --clipboard Operate on the CLIPBOARD selection
-k, --keep Do not modify the selections, but make the PRIMARY
and SECONDARY selections persist even after the
programs they were selected in exit.
-x, --exchange Exchange the PRIMARY and SECONDARY selections
X options
--display displayname
Specify the connection to the X server
-t ms, --selectionTimeout ms
Specify the timeout in milliseconds within which the
selection must be retrieved. A value of 0 (zero)
specifies no timeout (default)
Miscellaneous options
-l, --logfile Specify file to log errors to when detached.
-n, --nodetach Do not detach from the controlling terminal. Without
this option, xsel will fork to become a background
process in input, exchange and keep modes.
-h, --help Display this help and exit
-v, --verbose Print informative messages
--version Output version information and exit
Please report bugs to <conrad@vergenet.net>.
bugfix
❌
Error: Can't open display: (null)
❌
Error: target STRING not available
https://stackoverflow.com/questions/39552546/xclip-check-if-selection-is-empty
https://github.com/astrand/xclip/issues/38
refs
https://ostechnix.com/how-to-use-pbcopy-and-pbpaste-commands-on-linux/
https://www.cnblogs.com/lepeCoder/p/7885895.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17318422.html
未经授权禁止转载,违者必究!