Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy current command at bash prompt to clipboard

Tags:

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

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.

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?

like image 882
Clayton Stanley Avatar asked Jan 05 '13 23:01

Clayton Stanley


People also ask

How do you copy the current line in terminal?

Press arrow up to get the line of code that you want. Then press CTRL+A to select it and CTRL+C to copy it into clipboard. Paste it with CTRL+V.

How do I copy in bash?

Ctrl+Shift+C and Ctrl+Shift+V If you highlight text in the terminal window with your mouse and hit Ctrl+Shift+C you'll copy that text into a clipboard buffer. You can use Ctrl+Shift+V to paste the copied text into the same terminal window, or into another terminal window.

How do you copy the clipboard command?

Copy and paste multiple items using the Office Clipboard Open the file that you want to copy items from. Select the first item that you want to copy, and press CTRL+C.


2 Answers

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-] then will copy whatever is on the current bash prompt to the clipboard.

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.

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

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.

like image 103
Clayton Stanley Avatar answered Oct 18 '22 11:10

Clayton Stanley


You can use READLINE_LINE with bind -x in bash 4:

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.

I also use this function to copy the last command:

cl() { history -p '!!'|tr -d \\n|pbcopy; } 
like image 43
Lri Avatar answered Oct 18 '22 10:10

Lri