Sign up ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I can see last command in terminal by pressing up arrow as in following example:

I typed cd /:

Makss-Mac:~ maks$ cd /

... pressed enter, command cd / executed and now I can press up arrow and see command cd / again:

Makss-Mac:/ maks$ cd /

I want not only see last command but copy it to clipboard.

Maybe exists shortcut that not only show last command in terminal (as up arrow do) but at the same time copy it to clipboard.

Or maybe there is shortcut for selecting all from current terminal line. And so I will be able to copy last command (after pressing up arrow) by pressing super+c. Of course I can select it by mouse. But when commands are in 10 times longer than cd / it can be too time-consuming, especially if you do it every 30 seconds.

Or maybe there is way to write plugin for Terminal

share|improve this question
It might be easier to find a solution here if you could add some details about what you intend to do with the content of the clipboard afterwards. Where are you going to paste it to? – patrix Nov 12 '13 at 18:40
yes to paste it to another terminal tab – Maxim Yefremov Nov 13 '13 at 3:28

3 Answers 3

up vote 7 down vote accepted

Put this into your ~/.bashrc ~/.bash_profile or where-ever you like it:

alias copyLastCmd='fc -ln -1 | awk '{$1=$1}1' | pbcopy '

After opening a new window (or running source ~/.bash_profile) you should be able to run copyLastCmd 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 with awk and then copy it into your Mac's pasteboard with pbcopy.


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:

shopt -s histappend
PROMPT_COMMAND='$PROMPT_COMMAND; history -a; history -n'

Then, once you have some new tabs open (let's call them "tab A" and "tab B"):

  1. Execute any command in tab A
  2. Switch to tab B, press enter on an empty line, giving you a "fresh" new line (and thus re-evaluating the history)
  3. use the up arrow once and you should have the command you've just entered in tab A.

EDIT 2: I've replaced the usage of sed with awk in the original answer above, to take care of both leading and trailing whitespace .

share|improve this answer
That's a great alias! To cut off the trailing new line, so you can edit the command after pasting, I used awk: alias copyLastCmd='fc -ln -1 | sed '1s/^[[:space:]]*//' | awk 1 ORS="" | pbcopy ' – nwinkler Nov 13 '13 at 14:48
@nwinkler: OH you're right, should've used awk: fc -ln -1 | awk '{$1=$1}1' will remove both trailing and leading whitespace. – Asmus Nov 13 '13 at 15:09

You can set your history to ignore commands that start with a space:

export HISTIGNORE=' *'

And then run the following command to git the last command to the clipboard (make sure you have a space at the beginning):

history 1 | cut -d " " -f 4- | pbcopy

You probably would then want to save the command as an alias to make it easier to remember.

share|improve this answer

Here's another take based on the answer by @Asmus. To make his alias work in my bash script, I had to use a bit of quoting, since single quotes don't work in a single-quoted alias without some additional quoting:

alias copyLastCmd='fc -ln -1 | awk '\''{$1=$1}1'\'' | pbcopy'

The '\'' will basically close the current quote, then add an escaped quote, and then open the actual quote again.

The above version will copy the last command to your clipboard, and once you paste it, it's immediately executed, since it will have a newline at the end.

To get rid of the newline at the end, so you can actually edit the command before running it, you can do the following:

alias copyLastCmd='fc -ln -1 | awk '\''{$1=$1}1'\'' ORS='\'''\'' | pbcopy'

The ORS='' (that's without the quoting) will strip off the trailing newline character, so you can edit the pasted text before running it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.