Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active January 4, 2026 11:31
Show Gist options
  • Select an option

  • Save chmouel/d2dd4ebea0782ce61d93e1d22a9995cf to your computer and use it in GitHub Desktop.

Select an option

Save chmouel/d2dd4ebea0782ce61d93e1d22a9995cf to your computer and use it in GitHub Desktop.
paste image from clipboard

Work with Claude Code / Gemini / Codex CLI

Enable Ctrl+V image pasting for AI agents (for example, Claude Code) on Linux and macOS so the agent can access the currently copied image.

Assume a tmp/ directory at the repository root (or it will fallback to /tmp) that is listed in .gitignore.

Add the following shell function to .zshrc or .bashrc. Ensure wl-paste, wtype, and notify-send are installed (typically available on standard Linux distributions).

Then add the corresponding configuration to Kitty.

map ctrl+alt+v            launch --type=background --cwd=current -- bash /path/to/pasteimage.sh

just press ctrl+alt+v when you are in claude code and it would show the [Image #1] thing:


 ▐▛███▜▌   Claude Code v2.0.33
▝▜█████▛▘  Sonnet 4.5 · API Usage Billing
  ▘▘ ▝▝    /home/chmouel/git/perso/stuff

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
> [Image #1] 
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  ⏵⏵ accept edits on (shift+tab to cycle)       
#!/usr/bin/env bash
# Author: Chmouel Boudjnah <[email protected]>
# With kitty:
# map ctrl+alt+v launch --type=background --cwd=current -- pasteimage
# requires: wl-clipboard, wtype, notify-send (for wayland linux) or osascript (for macOS)
set -eufo pipefail
gui=yes
msg=
function groot() {
git rev-parse --show-toplevel
}
get_clipboard_image() {
local output=$1
if [[ "$OSTYPE" == "darwin"* ]]; then
osascript <<EOF 2>/dev/null && return 0
try
set image_data to the clipboard as «class PNGf»
on error
error number 1
end try
set outFile to POSIX file "${output}"
set fp to open for access outFile with write permission
set eof of fp to 0
write image_data to fp
close access fp
EOF
elif command -v wl-paste &>/dev/null; then
wl-paste --type image/png >"${output}" 2>/dev/null && return 0
elif command -v xclip &>/dev/null; then
xclip -selection clipboard -t image/png -o >"${output}" 2>/dev/null && return 0
fi
return 1
}
help() {
echo "usage: $(basename $0)"
echo
grep ')\s\+##' "$0" | sed 's/^\s*/-/;s/)\s*/ /;s/^\s\+//;s/##//'
}
while getopts "hG" opt; do
case $opt in
G) ## Do not use GUI notifications
gui=
;;
h) ## Show this help
help
exit 0
;;
*)
echo "Unknown flag: -$OPTARG" >&2
help
exit 1
;;
esac
done
shift $((OPTIND - 1))
destdir=/tmp/s
rootdir=$(git rev-parse --show-toplevel 2>/dev/null || echo "")
if [[ -n ${rootdir} ]] && [[ -d ${rootdir}/tmp ]]; then
destdir=${rootdir}/${destdir}
fi
mkdir -p ${destdir}
# Clean up PNG files older than 1 day
find ${destdir} -name "*.png" -mtime +1 -delete 2>/dev/null || true
mkdir -p ${destdir} || {
msg="Could not create ${destdir} directory"
if [[ -n ${gui} ]]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
osascript -e "display notification \"${msg}\" with title \"pasteimage\""
else
notify-send pasteimage "${msg}"
fi
else
echo "${msg}" >&2
fi
exit 1
}
random="${destdir}/$(mktemp -u pasted-image-XXXXXX).png"
if get_clipboard_image "${random}"; then
frandom=$(readlink -f "${random}")
if [[ -n ${gui} ]]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
osascript -e "display notification \"Image saved to $(realpath --relative-to="$(groot)" "${random}") and path copied to clipboard\" with title \"pasteimage\""
echo -n "${frandom}" | pbcopy
osascript -e 'tell application "System Events" to keystroke "v" using command down'
else
notify-send pasteimage "Image saved to $(realpath --relative-to="$(groot)" "${random}") and path copied to clipboard"
echo -n "${frandom}" | wl-copy
wtype -M ctrl -M shift v
fi
else
echo "${frandom}"
fi
else
rm -f "${random}"
msg="Could not find image in clipboard"
if [[ -n ${gui} ]]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
osascript -e "display notification \"${msg}\" with title \"pasteimage\""
else
notify-send pasteimage "${msg}"
fi
else
echo "${msg}" >&2
fi
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment