CLI - Copy last command to clipboard - OS X

CLI - Copy last command to clipboard - OS X

I spend a decent amount of time in the command line and I’m always looking for better ways to do simple tasks. One of these tasks is working with the command `history`.

The command `history` will show you the last 500 commands you have executed. In it’s output it will prefix the command with a number at first this number isn’t very useful but with a few commands you can really leverage it to your advantage.

Let’s say we have these commands that history has outputted

$ history
  550  ls
  551  cd ..
  552  ls
  553  pw
  554  pw
  555  pwd
  556  cd ..
  557  ls
  558  ls -rlta
$

And let’s say the command is REALLY long and we don’t want to retype it, you can re-run the command using ! and specify the number the command is. For this example we’ll use 558

$ !558
ls -rlta
total 0
drwxr-xr-x+ 97 jasontucker  wheel  3298 Oct 15 09:15 ../
drwxr-xr-x   2 jasontucker  wheel    68 Oct 15 09:15 ./
$

What if you want to run just the last command, but you don’t want to look up its history id? !! will execute the last command.

$ !!
ls -rlta
total 0
drwxr-xr-x+ 97 jasontucker  wheel  3298 Oct 15 09:15 ../
drwxr-xr-x   2 jasontucker  wheel    68 Oct 15 09:15 ./
$

What if you want to copy the last command to the clipboard? (source)

$ echo "!!" | pbcopy

pbcopy? What is that?

$ man pbcopy

NAME
       pbcopy, pbpaste - provide copying and pasting to the pasteboard (the Clipboard) from command line

SYNOPSIS
       pbcopy [-help] [-pboard {general | ruler | find | font}]

       pbpaste [-help] [-pboard {general | ruler | find | font}] [-Prefer {txt | rtf | ps}]

DESCRIPTION
       pbcopy  takes the standard input and places it in the specified pasteboard. If no pasteboard is specified, the general pasteboard will be used by default.  The input is placed in the
       pasteboard as plain text data unless it begins with the Encapsulated PostScript (EPS) file header or the Rich Text Format (RTF) file header, in which case it is placed in the  paste-
       board as one of those data types.
...

Wait, there is a pbpaste too?

       pbpaste removes the data from the pasteboard and writes it to the standard output.  It normally looks first for plain text data in the pasteboard and writes that to the standard out-
       put; if no plain text data is in the pasteboard it looks for Encapsulated PostScript; if no EPS is present it looks for Rich Text.  If none of those types is present  in  the  paste-
       board, pbpaste produces no output.

So what can you do with pbpaste then? Let’s say you copied the code above and wanted to paste it into the terminal but with some finesse. You would highlight and copy the text then go to the terminal and type in pbpaste and press enter from there the command would be pasted and executed.

That’s it for today, more command line trickery is still up my sleeve. Let me know in the comments below what tricks you use with `history` `pbcopy` and `pbpaste`.