How can I copy the output of the last executed command directly into my clipboard?

2.5k views Asked by At

I have seen this post: how-can-i-copy-the-output-of-a-command-directly-into-my-clipboard

I have got this alias

# Redoes the last command and copies the output to clipboard

  alias cl="fc -e -|pbcopy"

However I would like an alias or a function that doesn't redo the last command, but does copy the output of the last command.

2

There are 2 answers

2
Yann Justdohit On

try this,

!-1 | pbcopy

or so if you want alias

alias cl="!-1 | pbcopy"
0
Michael Hamilton On

Shells in common use on Linux (such as bash) do not preserve the output from the previous command, so re-execution is the only solution unless you add code to do the preservation yourself. Pbcopy isn't always installed, xclip seems more commonly installed:

    ls -l
    !! | xclip