将 bash 提示符下的当前命令复制到剪贴板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14177700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy current command at bash prompt to clipboard
提问by Clayton Stanley
I would like a quick keyboard command sequence to copy the current command at a bash prompt to the clipboard.
我想要一个快速的键盘命令序列来将 bash 提示符下的当前命令复制到剪贴板。
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 命令复制到剪贴板,我会按 up+[some command sequence]复制它。或者,例如,要在 bash 历史中搜索命令,我会使用 ctrl+r,搜索,在命令提示符上显示它,然后[some command sequence]复制它,等等。
My current solution is using bash pipes: Pipe to/from the clipboard
我目前的解决方案是使用 bash 管道:管道到/从剪贴板
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?
实现我在这里想要实现的目标的正确方法是什么?
采纳答案by Clayton Stanley
Taking @Lauri's post for inspiration, here's a solution using the bind command:
以@Lauri 的帖子为灵感,这里有一个使用 bind 命令的解决方案:
bind '"\C-]":"\C-e\C-u pbcopy <<"EOF"\n\C-y\nEOF\n"'
ctrl-] then will copy whatever is on the current bash prompt to the clipboard.
ctrl-] 然后会将当前 bash 提示中的任何内容复制到剪贴板。
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.
为了使其持久化,您可以将上述绑定命令~/.bashrc添加到您的~/.inputrc.
Non-OS-X users will have to swap pbcopy out with the appropriate command, probably xclip.
非 OS-X 用户必须使用适当的命令(可能是 xclip)将 pbcopy 换出。
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.
使用引用的 heredoc 代替 echo+pipe 技术,以便保留 bash 提示符下命令中的单引号和双引号。例如,使用这种技术,我能够点击 ctrl-],从终端提示符复制实际的绑定命令,并将其粘贴到答案中。所以heredoc 技术在这里处理bind 命令中的所有特殊字符。
回答by Lri
You can use READLINE_LINEwith bind -xin bash 4:
您可以在 bash 4 中使用READLINE_LINEwith bind -x:
copyline() { printf %s "$READLINE_LINE"|pbcopy; }
bind -x '"\C-xc":copyline'
You can install bash 4 and make it the default login shell by running brew install bash;echo /usr/local/bin/bash|sudo tee -a /etc/shells;chsh -s /usr/local/bin/bash.
您可以安装 bash 4 并通过运行brew install bash;echo /usr/local/bin/bash|sudo tee -a /etc/shells;chsh -s /usr/local/bin/bash.
I also use this function to copy the last command:
我也用这个函数来复制最后一个命令:
cl() { history -p '!!'|tr -d \n|pbcopy; }
回答by laus102
I spent a decent amount of time today writing a simple zshimplementation for macOS; usage is as follows:
我今天花了很多时间为macOS编写一个简单的zsh实现;用法如下:
example command: git commit -m "Changed a few things"
command that copies: c git commit -m "Changed a few things"
# The second command does not actually execute the command, it just copies it.
# Using zsh, this should reduce the whole process to about 3 keystrokes:
#
# 1) CTRL + A (to go to the beginning of the line)
# 2) 'c' + ' '
# 3) ENTER
preexec()is a zsh hook function that gets called right when you press enter, but before the command actually executes.
preexec()是一个 zsh 钩子函数,当您按 Enter 键时会立即调用它,但在命令实际执行之前。
Since zsh strips arguments of certain characters like ' " ', we will want to use preexec(), which allows you to access the unprocessed, original command.
由于 zsh 去除了某些字符的参数,例如 '" ',我们将要使用preexec(),它允许您访问未处理的原始命令。
Pseudocode goes like this:
伪代码是这样的:
1) Make sure the command has 'c 'in the beginning
1)确保命令'c '在开头
2) If it does, copy the whole command, char by char, to a temp variable
2) 如果是,将整个命令逐个字符复制到临时变量
3) Pipe the temp variable into pbcopy, macOS's copy buffer
3) 将临时变量通过管道传送到pbcopymacOS 的复制缓冲区
Real code:
真实代码:
c() {} # you'll want this so that you don't get a command unrecognized error
preexec() {
tmp="";
if [ "${1:0:1}" = "c" ] && [ "${1:1:1}" = " " ] && [ "${1:2:1}" != " " ]; then
for (( i=2; i<${#1}; i++ )); do
tmp="${tmp}${1:$i:1}";
done
echo "$tmp" | pbcopy;
fi
}
Go ahead and stick the two aforementioned functions in your .zshrc file, or wherever you want (I put mine in a file in my .oh-my-zsh/customdirectory).
继续将上述两个函数粘贴到您的 .zshrc 文件中,或者您想要的任何地方(我将我的放在我.oh-my-zsh/custom目录中的一个文件中)。
If anyone has a more elegant solution, plz speak up. Anything to avoid using the mouse.
如果有人有更优雅的解决方案,请说出来。 避免使用鼠标的任何事情。
回答by plesiv
If xselis installed on your system you can add this in .inputrc:
如果xsel安装在您的系统上,您可以在.inputrc 中添加:
C-]: '\C-e\C-ucat <<EOF | tr -d "\n" | xsel -ib\n\C-y\nEOF\n'
Alternatively, if xclipis installed you could add this:
或者,如果xclip已安装,您可以添加以下内容:
C-]: '\C-e\C-ucat <<EOF | tr -d "\n" | xclip -se c\n\C-y\nEOF\n'
Notice:Used code from @Clayton's answer.
注意:使用了@Clayton 回答中的代码。
回答by sarpu
I use historyto find the command number that I am looking for, then I do:
我history用来查找我正在寻找的命令号,然后我做:
echo "!command_number" | xclip -in

