I can see the last command in the terminal by pressing up arrow
as in the following example:
I typed cd /
:
Makss-Mac:~ maks$ cd /
… pressed enter
, command cd /
executed and now I can press up arrow
and see the command cd /
again:
Makss-Mac:/ maks$ cd /
I want to not only see the last command but also copy it to the clipboard.
Maybe there exists a shortcut that not only shows the last command in the terminal (as up arrow
does) but at the same time copies it to the clipboard.
Or maybe there is a shortcut for selecting all from current terminal line. And so I would be able to copy the last command (after pressing up arrow
) by pressing super+c
. Of course I can select it with the mouse. But when commands are 10 times longer than cd /
it can be too time-consuming, especially if you do it every 30 seconds.
Or is there maybe a way to write a plugin for the terminal?
Best Answer
Put this into your
~/.bashrc
~/.bash_profile
or where-ever you like it:After opening a new window (or running
source ~/.bash_profile
) you should be able to runcopyLastCmd
and have the command in the clipboard.To explain what's going on: you're basically using "fix command" (
fc
) to get you the last command, remove both leading and trailing whitespace for nicer formatting withawk
and then copy it into your Mac's pasteboard withpbcopy
.EDIT:
Now that we know that you want to copy to paste into another terminal tab, there's another option: Add these lines to your
~/.bashrc
~/.bash_profile
or where-ever you like it:Then, once you have some new tabs open (let's call them "tab A" and "tab B"):
EDIT 2: I've replaced the usage of
sed
withawk
in the original answer above, to take care of both leading and trailing whitespace .