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.

2 Answers 2

8

As a one-off for the last command:

echo !! | pbcopy 

will put exactly the last command into the clipboard.

1
0

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

history 2 | xclip
3
  • 1
    xclip requires a X11 session... Feb 28, 2012 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, 2012 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, 2016 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .