Skip to content

Instantly share code, notes, and snippets.

@pvdb
Last active September 26, 2026 09:04
Show Gist options
  • Select an option

  • Save pvdb/df70531abbdc3b98f22cfcf7c352b59f to your computer and use it in GitHub Desktop.

Select an option

Save pvdb/df70531abbdc3b98f22cfcf7c352b59f to your computer and use it in GitHub Desktop.
Run clipboard as shell commands, copy transcript back — for AI agent round-trips
#!/usr/bin/env bash
#
# pbexec - runs each line from the macOS clipboard as a shell command
# showing a colorized live transcript, and copies the full transcript
# (commands incl. output) back to the clipboard - for pasting results
# back into an AI agent that suggested the commands
#
# WARNING: executes clipboard contents with no confirmation step
# only run this after reading what's on your clipboard!
#
# INSTALLATION
#
# ln -s ${PWD}/pbexec $(brew --prefix)/bin/
# sudo ln -s ${PWD}/pbexec /usr/local/bin/
#
# DEPENDENCIES
#
# macOS (pbpaste/pbcopy)
#
(
PS1='$ ' ; # default prompt to use for each line of input
ESC=$'\033' ; # for injecting ANSI escape codes
BOLD="${ESC}[1m" ; # "symbolic" bold ANSI code
CYAN="${ESC}[36m" ; # "symbolic" cyan ANSI code
RESET="${ESC}[0m" ; # "symbolic" reset ANSI code
# read AI agent instructions from the pasteboard
( pbpaste; printf '\n'; ) | (
while IFS= read -r line || [ -n "${line}" ]; do
# manually fold trailing-backslash / trailing-pipe continuations,
# _without_ touching/corrupting backslashes anywhere else in the
# line (known limitation: purely textual -- doesn't understand
# quoting, so other shell continuation forms, e.g. &&, ||, {,
# unclosed (, are not (yet) folded)
while [[ "${line}" == *\\ || "${line}" == *'|' ]] ; do
if IFS= read -r next_line; then
if [[ "${line}" == *\\ ]]; then
line="${line%\\}${next_line}" ; # backslash: strip marker, join
else
line="${line}${next_line}" ; # pipe: keep as-is, join
fi
else
break ;
fi
done
if [ -n "${line}" ]; then
printf '%s%s\n' "${PS1}" "${line}" ;
printf '%s' "${line}" | 2>&1 sh ;
printf '%s_\n\n' "${PS1}" ;
fi
done
) | tee >( > /dev/tty \
sed -E "s/^\\${PS1}(.*|_)$/${BOLD}\\${PS1}${RESET}${CYAN}\\1${RESET}/"
) | pbcopy ; # all output ready to be pasted back into the AI agent
)
# That's all Folks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment