Bob Nadler, Jr. Bob Nadler, Jr.

OS X pbcopy

Published over 8 years ago less than 1 min read
Copy-Paste
Image by Tim Bartel

OS X has a nifty utility program named pbcopy that I just learned about which can be used to copy things to the system clipboard.

For example: cat foo.txt | pbcopy or pbcopy < foo.txt will copy the contents of foo.txt onto the system clipboard.

Bonus Vim Tip #1

This can be used in vim as well.

For example, to copy the current buffer’s filename onto the system clipboard:

:Dispatch! echo -n “%” | pbcopy

  ^             ^          ^
  |             |          |— copy it to the clipboard
  |             |
  |             |— echo the current buffer’s filename
  |
  |— Run command in the background

Bonus Vim Tip #2

This also works with ranges. Need to copy something from a vim buffer without those annoying line numbers or tmux dots? You can do something like this:

:9,24w !pbcopy

  ^      ^
  |      |— put them on the system clipboard
  |
  |— copy lines 9-24 of the current buffer

Share This Article