Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mac clipboard manipulation scripts I use the most. These all live in my ~/bin/ directory
#!/bin/bash
# Useful for when you want to trim off leading spaces from clipboard text
# Say you grab a chunk of code from a markdown document and want to remove
# the four spaces at the front:
#
# pbcut -c 5-
pbpaste | cut "$@" | pbcopy
pbpaste
#!/bin/bash
# Useful for when you want to add spaces in front of some text. Perhaps
# you want something to appear as code in a Markdown document, just run
# pbindent twice. Indented text also looks nice in emails to make something
# appear as a quote of a document or contents of a file.
pbpaste | perl -pe 's/^/ /' | pbcopy
#!/bin/bash
# Will prepend the given prefix onto each line of the clipboard.
# Very useful for when you have some text you want to turn into
# a bulleted list.
#
# pbprefix " - "
#
# Maybe you want some text to appear as quoted in an email:
#
# pbprefix "> "
#
PREFIX="${1?Specify a prefix such as a dash or bullet}"
pbpaste | perl -pe "s/^/$PREFIX/" | pbcopy
#!/bin/bash
# This clipboard text would be better if it was sorted, right?
# Options can be passed to the sort command, so for example to
# sort the clipboard text numerically:
#
# pbsort -n
#
pbpaste | sort "$@" | pbcopy
#!/bin/bash
# For when you don't want the fonts, colors and other fancy
# formatting information. Yes, there are hot keys for that, but
# I live in the terminal anyway and have all these other commands
# so why not.
pbpaste | pbcopy
#!/bin/bash
# Would it be nice if this text was word-wrapped? Sure,
# we can paste it into emacs, wrap it, then copy it back,
# but perhaps this is a bit quicker.
#
# pbwrap
# pbwrap 77
#
WIDTH="${1:-90}"
echo $(pbpaste) | fold -w "$WIDTH" -s | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment