Skip to content

Instantly share code, notes, and snippets.

@danpaldev
Forked from YumaInaura/.README.md
Created December 16, 2020 21:11
Show Gist options
  • Save danpaldev/78ae90f8bff5e919da719c5d7971cf94 to your computer and use it in GitHub Desktop.
Save danpaldev/78ae90f8bff5e919da719c5d7971cf94 to your computer and use it in GitHub Desktop.
Mac — Replace clipboard text with regex in console

Mac — Replace clipboard text with regex in console

I noticed today.

We can edit Mac's clipboad directly by pbcopy and pbpaste commands combination.

pbpaste | sed 's/Bob//g' | pbcopy
  • pbpaste stdout text in clipboard
  • sed get stdin, edit it and stdout
  • pboaste get edited text from stdin

Example : Simple replace

echo "AliceBobCarol" | pbcopy && pbpaste | sed 's/Bob//g' | pbcopy && pbpaste
AliceCarol

Example : Regular expression

Use gsed command. Because mac default 'sed' command is too poor can not use regex.

$ brew install gnu-sed
$ echo "AliceAliceBobCarol" | pbcopy && pbpaste | gsed --regexp-extended 's/^(Alice)+//g' | pbcopy && pbpaste
BobCarol

Now

You got a new clipboard text and can paste to any place.

image

Be careful

When missed replace, clipboad text will be runexpected.(obvious thing)

If improve this method, I should change command. For example create temporary file, make that chained command to one command.

And recommend

Alfred's clipboad extension is very nice tool. You can keep clipboad history and paste them anytime.

image

Versions

  • Mac OS X High Sierra

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment