Skip to content

Instantly share code, notes, and snippets.

@Davenchy
Created May 13, 2024 02:31
Show Gist options
  • Save Davenchy/e62501f65706f2909f7d527ebcd569f5 to your computer and use it in GitHub Desktop.
Save Davenchy/e62501f65706f2909f7d527ebcd569f5 to your computer and use it in GitHub Desktop.
Copy/Paste bash scripts using xclip
#!/usr/bin/bash
# Simple bash script to copy files into clipboard using xclip
# Check if xclip is installed
if ! command -v xclip &> /dev/null; then
echo "Error: xclip is not installed. Please install xclip before running this script."
exit 1
fi
if [[ -z "$1" ]]; then
# validate if file was passed
echo "Usage: xcopy <file> [<mime>]"
exit 1
fi
if [[ -z "$2" ]]; then
# if no mimetype passed, determine it using file command
mime=$(file -ib "$1" | cut -d';' -f1)
# print used mimetype
echo "Detected MimeType: $mime"
else
# use passed mimetype
mime=$2
fi
# copy file using mimetype
xclip -sel clip -target "$mime" -i < "$1"
#!/usr/bin/bash
# Simple bash script to paste files from clipboard using xclip
# Check if xclip is installed
if ! command -v xclip &> /dev/null; then
echo "Error: xclip is not installed. Please install xclip before running this script."
exit 1
fi
if [[ "$#" -lt 2 ]]; then
# validate if file was passed
echo "Usage: xpaste <out-file> <mime>"
exit 1
fi
# paste file from clipboad using xclip
xclip -sel clip -target "$2" -o > "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment