Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Last active February 6, 2026 11:17
Show Gist options
  • Select an option

  • Save kidpixo/2f6a7d7c763b0d85e989e37487a2a484 to your computer and use it in GitHub Desktop.

Select an option

Save kidpixo/2f6a7d7c763b0d85e989e37487a2a484 to your computer and use it in GitHub Desktop.
bash script to capture shell output to clipboard, optionally the original command and print to stdout too.
clipify() {
local stdout=false
local capture_cmd=false
local OPTIND usage
usage="Usage: command | clipify [OPTIONS]
(Always copies to clipboard)
-a All: Include the command itself in the capture
-s Screen: Also print the final content to terminal (tee)
-h Display this help
Note: Avoid large files (>100MB), binary data, ANSI colors,
or subshells."
# Detect if we are being piped into
if [[ -t 0 ]]; then
echo -e "Error: clipify requires input from a pipe.\n"
echo "$usage"
return 1
fi
while getopts "ash" opt; do
case "$opt" in
a) capture_cmd=true ;;
s) stdout=true ;;
h) echo "$usage"; return 0 ;;
*) echo "$usage"; return 1 ;;
esac
done
# 1. Capture the piped input
local input=$(cat)
[[ -z "$input" ]] && return 0
local final_content="$input"
# 2. Composition Logic: If -a is set, wrap the content
if [[ "$capture_cmd" == true ]]; then
local last_cmd=$(history | tail -n 1 | sed 's/^[ ]*[0-9]*[ ]*//; s/[ ]*|[ ]*clipify.*//')
final_content=$(printf "COMMAND:\n%s\n\nOUTPUT:\n%s" "$last_cmd" "$input")
fi
# 3. Always copy to clipboard (The "c" behavior is now default)
if command -v xclip >/dev/null; then
printf "%s" "$final_content" | xclip -sel clip
elif command -v pbcopy >/dev/null; then
printf "%s" "$final_content" | pbcopy
fi
# 4. If -s is set, also emit to stdout
if [[ "$stdout" == true ]]; then
printf "%s\n" "$final_content"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment