Skip to content

Instantly share code, notes, and snippets.

@unascribed
Last active April 4, 2020 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unascribed/7bd4e276cb2555f91d957158cce4164a to your computer and use it in GitHub Desktop.
Save unascribed/7bd4e276cb2555f91d957158cce4164a to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ "$1" == "-h" || "$1" == "-?" || "$1" == "--help" ]]; then
(
echo 'copycat: copy stdin or files specified on command line to Wayland and/or X clipboard'
echo 'performs automatic mimetype detection like wl-copy via file under X'
echo 'usage:'
echo ' copycat: copy stdin to clipboard'
echo ' copycat <-h|--help|-?>: display usage help'
echo ' copycat <files...>: concatenate each file, then copy to clipboard'
echo
echo 'if $WAYLAND_DISPLAY is set, delegates to wl-copy'
echo 'otherwise, uses xclip and file'
echo 'similarly to cat, - may be specified as a filename to allow concatening stdin with files'
) 1>&2
exit 0
fi
if [ -n "$WAYLAND_DISPLAY" ]; then
cat -- "$@" | wl-copy
echo "copycat: copied to Wayland clipboard" 1>&2
else
tmpfile=$(mktemp)
cat -- "$@" > "$tmpfile"
mime=$(file -b --mime-type "$tmpfile")
xclip -t "$mime" -selection clipboard "$tmpfile"
rm "$tmpfile"
echo "copycat: copied to X clipboard, detected mimetype $mime" 1>&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment