用于粘贴我的命令及其输出的命令

Pau*_*jan 9 command-line clipboard

很多时候我想发布一些东西到一个 github 错误,比如

$ ping google.com
PING google.com (216.58.195.238): 56 data bytes
Request timeout for icmp_seq 0
64 bytes from 216.58.195.238: icmp_seq=0 ttl=53 time=1064.747 ms
Run Code Online (Sandbox Code Playgroud)

现在我运行命令,使用screen'sC-a C-[突出显示该区域,enter将其复制到该缓冲区,将其粘贴到vim,将其写入文件,然后将cat其写入pbcopy. 一定有更好的方法。

有没有我可以运行tee的命令,我输入的命令是否以 a 为前缀,$所有输出都为pbcopy?或者任何接近的东西?我设想

$ demo ping google.com
PING google.com (216.58.195.238): 56 data bytes
Request timeout for icmp_seq 0
64 bytes from 216.58.195.238: icmp_seq=0 ttl=53 time=1064.747 ms
^C
$
Run Code Online (Sandbox Code Playgroud)

现在我粘贴的原始内容在我的 mac 剪贴板中。

JRF*_*son 10

一种选择是-x在运行命令的子 shell 中启用跟踪(使用)。这会将命令写入 STDERR,将标准输出写入 STDOUT。收集并管道到pbcopy

$ ( set -x; ping -c 3 google.com ) 2>&1 | pbcopy

$ pbpaste
+ ping -c 3 google.com
PING google.com (173.194.217.138): 56 data bytes
64 bytes from 173.194.217.138: icmp_seq=0 ttl=44 time=37.436 ms
64 bytes from 173.194.217.138: icmp_seq=1 ttl=44 time=38.891 ms
64 bytes from 173.194.217.138: icmp_seq=2 ttl=44 time=39.329 ms
--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 37.436/38.552/39.329/0.809 ms
Run Code Online (Sandbox Code Playgroud)


Dop*_*oti 7

您可以使用该script实用程序来捕获整个交互,包括您的提示、命令及其输出:

script temp.log; cat temp.log | pbcopy
[ do stuff ]
[ end the interaction with ^D or logging out of the shell ]
Run Code Online (Sandbox Code Playgroud)

然后您可以查看该文件,其内容将已在您的剪贴板上。

你也可以:

script temp.log 'somecommand'; cat temp.log | pbcopy
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用 `script temp.log env PS1='$ ' bash --norc` 来获得带有您想要的提示的 bash。或者你可以编辑你的 `~/.bashrc` 来检查 `script` 的使用并改变提示:`[ $(ps -o command= -p $PPID) = 'script' ] && PS1='$ ' ` (2认同)