gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

feat: Add clipboard image pasting with cross-platform support

Open prajwalr308 opened this issue 1 month ago • 2 comments

Summary

Enables users to paste images directly from their clipboard into the Gemini CLI using Ctrl+V or Shift+Insert. Images are automatically saved to .gemini-clipboard/ directory with sequential numbering and displayed with clean [image #N] labels that convert to file references when submitted.

This feature provides seamless image sharing across all platforms including macOS, Windows, Linux, and WSL environments.

Details

Key Features:

  • Cross-platform clipboard support: Works on macOS, Windows, Linux, and WSL
  • WSL PowerShell integration: Detects WSL environments and uses PowerShell for clipboard access
  • Sequential image numbering: Automatically names images as image-1.png, image-2.png, etc.
  • Clean UI display: Shows [image #1] in the input, converts to @.gemini-clipboard/image-1.png on submit
  • Multiple keyboard shortcuts: Supports both Ctrl+V and Shift+Insert
  • Format detection: Automatically detects and preserves image formats (PNG, JPG, GIF, BMP, WebP, TIFF)
  • Auto-cleanup: Removes clipboard images older than 1 hour
  • Comprehensive testing: Full test coverage for all clipboard utilities

Implementation:

  • New clipboardUtils.ts with clipboard detection and image saving logic
  • WSL detection and PowerShell integration for Windows clipboard access
  • Enhanced InputPrompt component to handle clipboard paste events
  • Added @crosscopy/clipboard package for cross-platform clipboard support
  • Image format detection via magic byte analysis

Files Changed:

  • packages/cli/src/ui/utils/clipboardUtils.ts - Core clipboard functionality
  • packages/cli/src/ui/components/InputPrompt.tsx - UI integration and display format
  • packages/cli/src/config/keyBindings.ts - Added Shift+Insert binding
  • packages/cli/src/ui/utils/screenshotUtils.ts - Screenshot utilities
  • Test files with comprehensive coverage

Related Issues

How to Validate

On macOS/Linux:

  1. Copy an image to clipboard (screenshot, browser image, etc.)
  2. Run npm run dev or npx @google/generative-ai-cli
  3. Press Ctrl+V or Shift+Insert in the input prompt
  4. Verify [image #1] appears in the input
  5. Submit the prompt and verify the image is sent to Gemini
  6. Check .gemini-clipboard/image-1.png exists

On Windows (native):

  1. Copy an image to clipboard
  2. Run the CLI via npm run dev or npx
  3. Press Ctrl+V and verify image paste works
  4. Verify sequential numbering with multiple images

On WSL:

  1. Copy image in Windows (e.g., Snipping Tool, browser)
  2. Open WSL terminal and run the CLI
  3. Press Ctrl+V in the CLI
  4. Verify PowerShell bridge correctly retrieves Windows clipboard image
  5. Check that .gemini-clipboard/image-N.png is created in WSL filesystem

Test sequential numbering:

  1. Paste multiple images one after another
  2. Verify they're numbered [image #1], [image #2], [image #3], etc.
  3. Delete one and paste again - should use next sequential number

Test cleanup:

  1. Create old test images with past timestamps in .gemini-clipboard/
  2. Paste a new image (triggers cleanup)
  3. Verify old images are removed

Pre-Merge Checklist

  • [x] Updated relevant documentation and README (if needed)
  • [x] Added/updated tests (comprehensive test coverage added)
  • [ ] Noted breaking changes (if any) - No breaking changes
  • [ ] Validated on required platforms/methods:
    • [ ] MacOS
      • [ ] npm run
      • [ ] npx
      • [ ] Docker
      • [ ] Podman
      • [ ] Seatbelt
    • [x] Windows
      • [x] npm run
      • [ ] npx
      • [ ] Docker
    • [x] Linux
      • [x] npm run
      • [ ] npx
      • [ ] Docker\

prajwalr308 avatar Nov 22 '25 11:11 prajwalr308

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Nov 22 '25 11:11 google-cla[bot]

Hey tests are failing, could you take a look and fix them?

SandyTao520 avatar Dec 01 '25 19:12 SandyTao520