Created
September 25, 2025 02:09
-
-
Save pszemraj/015f032194dfdd7782160afb89ac64f4 to your computer and use it in GitHub Desktop.
cz() two letter clipboard helper for linux/xclip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copy file contents or stdin to clipboard | |
| # Usage: cz [file] | |
| # cz file.txt - copy file to clipboard | |
| # cmd | cz - copy stdin to clipboard | |
| # Fails on: binary files, files >10MB, non-existent files | |
| cz() { | |
| if [ -z "$1" ]; then | |
| xclip -selection clipboard | |
| elif [ -f "$1" ]; then | |
| # Check if it's a text file | |
| if file -b --mime-type "$1" | grep -q '^text/'; then | |
| # Check size (bail if over 10MB) | |
| size=$(stat -c%s "$1" 2>/dev/null || stat -f%z "$1" 2>/dev/null) | |
| if [ "$size" -gt 10485760 ]; then | |
| echo "Error: '$1' is ${size} bytes (>10MB). Too large for clipboard" >&2 | |
| return 1 | |
| fi | |
| xclip -selection clipboard < "$1" | |
| else | |
| echo "Error: '$1' is not a text file" >&2 | |
| return 1 | |
| fi | |
| else | |
| echo "Error: '$1' not found" >&2 | |
| return 1 | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment