nikivdev/code · error

clipboard command exited with status {}

Error message

clipboard command exited with status {}

What it means

On Linux the clipboard helper tries `xclip` first, then `xsel`, invoking whichever exists and writing text to its stdin. If the chosen command exits nonzero, the code bails with its exit status. Unlike the missing-binary cases, the command was found and executed but failed to place text on the X11/Wayland clipboard.

Source

Thrown at src/ai.rs:14196

            .spawn();

        let mut child = match result {
            Ok(c) => c,
            Err(_) => Command::new("xsel")
                .arg("--clipboard")
                .arg("--input")
                .stdin(Stdio::piped())
                .spawn()
                .context("failed to spawn xclip or xsel")?,
        };

        if let Some(stdin) = child.stdin.as_mut() {
            stdin.write_all(text.as_bytes())?;
        }

        let status = child.wait()?;
        if !status.success() {
            bail!("clipboard command exited with status {}", status);
        }
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        bail!("clipboard not supported on this platform");
    }

    Ok(())
}

/// Strip <thinking> blocks from content (internal Claude processing).
fn strip_thinking_blocks(s: &str) -> String {
    let mut remaining = s;
    let mut out = String::new();

    loop {
        let Some(start) = remaining.find("<thinking>") else {

View on GitHub (pinned to a747e741ae)

Solutions

  1. Ensure a display is available: check `echo $DISPLAY` and set DISPLAY/XAUTHORITY if running from SSH or a service
  2. Install a working clipboard tool: `apt install xclip` or use `wl-copy` (wl-clipboard) on Wayland
  3. Test directly: `echo test | xclip -selection clipboard` to reproduce the failure outside the tool
  4. Run the command inside the graphical session (e.g. via the desktop terminal) instead of a headless context

Example fix

// before (SSH, no DISPLAY)
$ echo hi | xclip -selection clipboard
Error: Can't open display: (null)
$ myapp ai copy last
Error: clipboard command exited with status 1
// after (inside desktop session or with wl-clipboard on Wayland)
$ echo hi | wl-copy
$ myapp ai copy last   # succeeds
Defensive patterns

Strategy: fallback

Validate before calling

use std::process::Command;
fn clipboard_cmd_available() -> bool {
    ["xclip", "xsel", "wl-copy"].iter().any(|c| {
        Command::new(c).arg("--version").output().is_ok()
            || Command::new(c).output().is_ok()
    })
}
if !clipboard_cmd_available() { eprintln!("install xclip/wl-clipboard"); }

Try / catch

match copy_to_clipboard(text) {
    Err(e) if e.to_string().starts_with("clipboard command exited") => {
        eprintln!("clipboard failed ({e}); ensure DISPLAY/XAUTHORITY are set or use wl-copy");
        println!("{text}");
    }
    Err(e) => return Err(e),
    Ok(()) => {}
}

Prevention

When it happens

Trigger: xclip/xsel cannot connect to an X display (DISPLAY unset) — e.g. SSH, systemd service, Wayland-only session without XWayland; the binary exists but crashes or is denied clipboard access; sandboxed/CI environments without a clipboard server.

Common situations: Wayland desktops where xclip targets X11 via XWayland and fails; SSH sessions to a Linux desktop; cron/systemd units lacking DISPLAY/XAUTHORITY; containers with no X server.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/a4928bc6db4e2075. Report an issue: GitHub.