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
p0=1
else
p0=0
fi
if [[ -t 0 ]]; then # stdin is a tty
t0=1
else
t0=0
fi
if [[ -t 1 ]]; then # stdout is a tty
t1=1
else
t1=0
fi
if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
cat > /dev/clipboard # so send it to the pasteboard
if [[ $t1 -eq 0 ]]; 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 pasteboard (enables `man tee` like chaining)
fi
else # stdin is not a pipe
cat /dev/clipboard # so output the pasteboard
if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
echo # prevent the prompt from being on the same line
fi
fi
#!/bin/bash
if [[ -p /dev/stdin ]]; then # stdin is a pipe
p0=1
else
p0=0
fi
if [[ -t 0 ]]; then # stdin is a tty
t0=1
else
t0=0
fi
if [[ -t 1 ]]; then # stdout is a tty
t1=1
else
t1=0
fi
if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
cat | xclip -selection clipboard # so send it to the pasteboard
if [[ $t1 -eq 0 ]]; 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
if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
echo # prevent the prompt from being on the same line
fi
fi
#!/bin/bash
if [[ -p /dev/stdin ]]; then # stdin is a pipe
p0=1
else
p0=0
fi
if [[ -t 0 ]]; then # stdin is a tty
t0=1
else
t0=0
fi
if [[ -t 1 ]]; then # stdout is a tty
t1=1
else
t1=0
fi
if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
cat | pbcopy # so send it to the pasteboard
if [[ $t1 -eq 0 ]]; 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
if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
echo # prevent the prompt from being on the same line
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment