Copy to Clipboard from Command Line

By  on  

If you've ever been debugging with a fellow developer, you'll hear "OK, execute that and let me know what it says".  In this case, you can either manually copy the output and instant message the text over to them, or you can write the output to file with >> , open the file, manually copy the contents, and paste it into IM.  I experience this for a few hours recently and it was way to much work!  Shouldn't there be a way to quickly place an execution's output directly into the clipboard just to save some time?  You can with pbcopy!

Copy stdout to Clipboard

You'll use a single pipe to transfer the stdout result into the clipboard:

# command | pbcopy
hg diff | pbcopy

The git diff information is copied to the clipboard in this example; now you can show your colleague what you've changed.

Copy File Contents to Clipboard

In the case of copying file contents into the clipboard, pbcopy goes first:

# pbcopy < file.ext
pbcopy < circle.yml

The complete file contents are instantly copied to the clipboard for easy sharing.

Pasting to File

So what if you want to paste the clipboard contents into a new or existing file?  Use pbpaste:

#pbpaste > file.txt
pbpaste > commands.txt

The clipboard contents will be placed into the given file.

pbcopy will be a big timesaver for me moving forward.  Manually copy and pasting information is with the mouse or trackpad is inconvenient and time-consuming.  These types of command line techniques can make us more proficient, skilled developers!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Vertically Centering with Flexbox

    Vertically centering sibling child contents is a task we've long needed on the web but has always seemed way more difficult than it should be.  We initially used tables to accomplish the task, then moved on to CSS and JavaScript tricks because table layout was horribly...

  • By
    MooTools 1.2 Image Protector: dwProtector

    Image protection is a hot topic on the net these days, and why shouldn't it be? If you spent two hours designing an awesome graphic, would you want it ripped of in matter of seconds? Hell no! That's why I've created an image...

Discussion

  1. Jason

    You may want to point out that pbcopy is Mac only.

    With Linux, you’ll need other tools like xclip.

  2. Mark Hargrove

    May want to mention this is an OS X thing, though I guess this is available as an add-on for Windows.

  3. Markus

    how to get/install pbcoppy? is it a mac thing?

    • Yann

      Yes, pbcopy and pbpaste are OS X commands.

      I’m sure it’s possible to find alternatives for your OS of choice.

  4. Chris

    On Windows (both cmd and Powershell) simply use

    echo 1 | clip

  5. On linux I have this two aliases:

    alias c-c='xclip -sel clip'
    alias c-v='xclip -o -sel clip'
    
  6. echo 1 | clip
    is only for one line
    for multiline u may do
    type file.txt | clip

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!