将bash提示符下的当前命令复制到剪贴板 [英] Copy current command at bash prompt to clipboard

查看:150
本文介绍了将bash提示符下的当前命令复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要快速的键盘命令序列,以将bash提示符下的当前命令复制到剪贴板.

I would like a quick keyboard command sequence to copy the current command at a bash prompt to the clipboard.

例如,要将最后一个bash命令复制到剪贴板,请按up + [some command sequence]进行复制.或者,例如,要在bash历史记录中搜索命令,我将使用ctrl + r进行搜索,将其显示在命令提示符下,然后按[some command sequence]进行复制,等等.

So that, for example, to copy the last bash command to the clipboard, I'd press up+[some command sequence] to copy it. Or, for example, to search for a command in bash hisory, I'd use ctrl+r, search, display it on the command prompt, and then [some command sequence] to copy it, etc.

我当前的解决方案是使用bash管道:从剪贴板插入管道

My current solution is using bash pipes: Pipe to/from the clipboard

因此,要将上一个命令复制到剪贴板:

So, to copy the previous command to clipboard:

echo "!!" | pbcopy

哪个不太可怕,但是如果要复制的命令不是最后一个命令,该怎么办?

Which isn't too terrible, but what if the command to copy isn't the last command, etc.

在这里实现我要实现的目标的正确方法是什么?

What's the proper way to achieve what I'm trying to achieve here?

推荐答案

以@Lauri的帖子为灵感,以下是使用bind命令的解决方案:

Taking @Lauri's post for inspiration, here's a solution using the bind command:

bind '"\C-]":"\C-e\C-u pbcopy <<"EOF"\n\C-y\nEOF\n"'

ctrl-]然后将当前bash提示上的内容复制到剪贴板.

ctrl-] then will copy whatever is on the current bash prompt to the clipboard.

要使其持久化,可以在~/.bashrc上添加上述的bind命令,也可以去除外引号并删除调用的"bind"部分,然后将结果添加到~/.inputrc中.

To make it persistent, you can add the bind command as above to your ~/.bashrc, or you can strip off the outer quotes and remove the 'bind' part of the call and add the result to your ~/.inputrc.

非OS-X用户必须使用适当的命令(可能是xclip)换出pbcopy.

Non-OS-X users will have to swap pbcopy out with the appropriate command, probably xclip.

使用带引号的heredoc代替了echo + pipe技术,以便保留bash提示符下命令中的单引号和双引号.例如,使用这种技术,我可以按ctrl-],从终端提示符下复制实际的bind命令,并将其粘贴到答案中.因此,heredoc技术可在此处处理bind命令中的所有特殊字符.

A quoted heredoc was used instead of a an echo+pipe technique so that both single and double quotes in the command at the bash prompt are preserved. With this technique, for example, I was able to hit ctrl-], copy the actual bind command from the terminal prompt, and paste it here in the answer. So the heredoc technique handles all of the special characters in the bind command here.

这篇关于将bash提示符下的当前命令复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆