Skip to content

Instantly share code, notes, and snippets.

@Frankz
Forked from RichardBronosky/README.MD
Created May 3, 2018 14:45
Show Gist options
  • Save Frankz/0112bf3cbd37e2218bafb2427e132034 to your computer and use it in GitHub Desktop.
Save Frankz/0112bf3cbd37e2218bafb2427e132034 to your computer and use it in GitHub Desktop.
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
_copy(){
cat > /dev/clipboard
}
_paste(){
cat /dev/clipboard
}
if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
_copy # so send it to the clipboard
if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
_paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
fi
else # stdin is not a pipe
_paste # so output the clipboard
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
_copy(){
cat | xclip -selection clipboard
}
_paste(){
xclip -selection clipboard -o
}
if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
_copy # so send it to the clipboard
if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
_paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
fi
else # stdin is not a pipe
_paste # so output the clipboard
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
_copy(){
cat | pbcopy
}
_paste(){
pbpaste
}
if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
_copy # so send it to the clipboard
if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
_paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
fi
else # stdin is not a pipe
_paste # so output the clipboard
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