Skip to content

Instantly share code, notes, and snippets.

@evil5hadow
Forked from RichardBronosky/README.MD
Created January 15, 2019 12:37
Show Gist options
  • Save evil5hadow/a55ce980ec79c44cadd061774d5b40d0 to your computer and use it in GitHub Desktop.
Save evil5hadow/a55ce980ec79c44cadd061774d5b40d0 to your computer and use it in GitHub Desktop.
cb - A leak-proof tee to the clipboard - Unify the copy and paste commands into one intelligent chainable command.

cb

A leak-proof tee to the clipboard

This script is modeled after tee (see man tee).

It's like your normal copy and paste commands, but unified and able to sense when you want it to be chainable

Examples

Copy

$ date | cb
# clipboard contains: Tue Jan 24 23:00:00 EST 2017

Paste

# clipboard retained from the previous block
$ cb
Tue Jan 24 23:00:00 EST 2017
$ cb | cat
Tue Jan 24 23:00:00 EST 2017
$ cb > foo
$ cat foo
Tue Jan 24 23:00:00 EST 2017

Chaining

$ date | cb | tee updates.log
Tue Jan 24 23:11:11 EST 2017
$ cat updates.log
Tue Jan 24 23:11:11 EST 2017
# clipboard contains: Tue Jan 24 23:11:11 EST 2017
#!/bin/bash
if [[ -p /dev/stdin ]]; then # stdin is a pipe
cat > /dev/clipboard # so send it to the clipboard
if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
cat /dev/clipboard # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
fi
else # stdin is not a pipe
cat /dev/clipboard # so output the clipboard
fi
#!/bin/bash
if [[ -p /dev/stdin ]]; then # stdin is a pipe
cat | xclip -selection clipboard # so send it to the pasteboard
if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
xclip -selection clipboard -o # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
fi
else # stdin is not a pipe
xclip -selection clipboard -o # so output the pasteboard
fi
#!/bin/bash
if [[ -p /dev/stdin ]]; then # stdin is a pipe
cat | pbcopy # so send it to the pasteboard
if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
pbpaste # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
fi
else # stdin is not a pipe
pbpaste # so output the pasteboard
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment