OpenCode Clipboard Image
Purpose
Automate the common flow where pasting an image into terminal chat does not work: extract image data from the clipboard, save it to a file, and provide a path the user can reference in OpenCode.
When To Use
- User says image paste is not working in OpenCode/TUI.
- User asks to "paste a screenshot" into OpenCode from clipboard.
- User wants a one-step workflow to save a clipboard image and attach it by path.
Expected Outcome
- Clipboard image is saved to
~/Pictures/opencode/clip-YYYYMMDD-HHMMSS.<ext>. - Agent returns the absolute file path.
- Agent gives the exact message pattern to use in OpenCode (for example:
@/home/user/Pictures/opencode/clip-....png).
Required Workflow
- Detect Linux session type (
X11orWayland) when relevant:
echo "${XDG_SESSION_TYPE:-unknown}"
- Ensure clipboard utility exists:
- X11: prefer
xclip. - Wayland: prefer
wl-paste(packagewl-clipboard).
If missing, provide the installation command for the detected distro, then continue.
Save clipboard image into a stable directory:
mkdir -p "$HOME/Pictures/opencode"
- Use the first available image MIME type, save with matching extension, and print final path.
Commands
Linux X11 (xclip)
mkdir -p "$HOME/Pictures/opencode"
mime="$(xclip -selection clipboard -t TARGETS -o | tr ' ' '\n' | grep '^image/' | head -n1)"
if [ -z "$mime" ]; then
echo "Clipboard does not currently contain an image. Copy an image first, then retry." >&2
exit 1
fi
case "$mime" in
image/png) ext="png" ;;
image/jpeg) ext="jpg" ;;
image/webp) ext="webp" ;;
image/gif) ext="gif" ;;
*) mime="image/png"; ext="png" ;;
esac
f="$HOME/Pictures/opencode/clip-$(date +%Y%m%d-%H%M%S).$ext"
xclip -selection clipboard -t "$mime" -o > "$f" && printf '%s\n' "$f"
Linux Wayland (wl-paste)
mkdir -p "$HOME/Pictures/opencode"
mime="$(wl-paste --list-types | grep '^image/' | head -n1)"
if [ -z "$mime" ]; then
echo "Clipboard does not currently contain an image. Copy an image first, then retry." >&2
exit 1
fi
case "$mime" in
image/png) ext="png" ;;
image/jpeg) ext="jpg" ;;
image/webp) ext="webp" ;;
image/gif) ext="gif" ;;
*) mime="image/png"; ext="png" ;;
esac
f="$HOME/Pictures/opencode/clip-$(date +%Y%m%d-%H%M%S).$ext"
wl-paste --type "$mime" > "$f" && printf '%s\n' "$f"
Install Hints
- Debian/Ubuntu (X11):
sudo apt update && sudo apt install -y xclip - Debian/Ubuntu (Wayland):
sudo apt update && sudo apt install -y wl-clipboard - Arch:
sudo pacman -S xcliporsudo pacman -S wl-clipboard - Fedora:
sudo dnf install -y xcliporsudo dnf install -y wl-clipboard
Response Template
After saving successfully, respond with:
- Saved:
<absolute_path> - Use in OpenCode:
@<absolute_path>
If the clipboard has no image MIME type, respond with:
- "Clipboard does not currently contain an image. Copy an image first, then retry."
Safety
- Do not delete or overwrite existing files.
- Use timestamped filenames only.
- Never assume root/sudo access; provide install command if dependency is missing.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.