4

It often happens to me, that I just used a command and I need to copy/paste it either to a different terminal window/IM or whatever.

On a Mac, I can use the pbcopy command to get something in clipboard, but I'm not sure how should I get to the command. What I've came up is

history | tail -n 1 | perl -ple 's/\d+\s+//' | pbcopy

which works, but it doesn't really seem like clean solution. Is there a better way to get to the last command? I'm using zsh, but I guess if there's a solution for bash it will work in zsh too.

Jakub Arnold
  • 1,744
  • 10
  • 26
  • 33

2 Answers2

8

As a one-off for the last command:

echo !! | pbcopy 

will put exactly the last command into the clipboard.

Sven
  • 98,649
  • 14
  • 180
  • 226
0

You can use xclip for this task
See here
Your can just pipe something into the clipboard like this:

history 2 | xclip
Niko S P
  • 1,182
  • 8
  • 16
  • 1
    xclip requires a X11 session... – Yanick Girouard Feb 28 '12 at 13:07
  • 1
    If you read the question carefully, you will notice this part of the first sentence: "I need to copy/paste it either to a different terminal window/IM or whatever.". Doesn't that imply that he has a X Session? – Niko S P Feb 28 '12 at 13:15
  • 1
    No, it implies he has a window session, which may be X, but it also may be unity, NeWS, Finder.app or any of the other gui interfaces one may have to a system. – hd1 Mar 23 '16 at 00:05