Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active December 16, 2020 21:11
Show Gist options
  • Save YumaInaura/32b6d6ba379d4a65d22c06e3f8d284c0 to your computer and use it in GitHub Desktop.
Save YumaInaura/32b6d6ba379d4a65d22c06e3f8d284c0 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