Skip to content

TUI: add undo-last-paste for composer drafts #17203

@dolzenko

Description

@dolzenko

Summary

Add a narrow undo last paste feature to the TUI composer.

This is not a full editor undo stack. It only reverts the most recent successful paste transaction, including paste side effects beyond raw text.

Problem

The TUI already treats paste as a first-class composer operation:

  • normal pasted text
  • oversized paste placeholders backed by pending_pastes
  • pasted image paths that become local image attachments
  • shortcut image paste via Ctrl+V / Alt+V

Today, if a paste is accidental or wrong, the recovery options are inconsistent:

  • plain text may be manually deleted
  • oversized paste placeholders are not a normal text edit
  • pasted image attachments are not just text
  • there is no single standard shortcut to “undo the paste I just did”

The existing Ctrl+U / Ctrl+K / Ctrl+W / Ctrl+Y behavior is helpful, but it is kill/yank behavior, not paste undo.

Proposal

Add a composer-level undo last paste shortcut:

  • primary binding: Ctrl+_
  • accept terminal-friendly aliases: Ctrl+/ and raw ^_

Behavior:

  • only undoes the most recent successful paste transaction
  • restores the entire pre-paste draft state
  • includes:
    • text
    • text elements
    • local image attachments
    • remote image URLs
    • mention bindings
    • pending_pastes
    • cursor position
  • cursor-only movement after paste should not invalidate undo
  • any content mutation after paste should invalidate it
  • single-level only
  • no redo
  • no general-purpose edit undo stack

Why composer-level, not textarea-level

Paste in the TUI is not only text insertion.

A general textarea undo would not naturally cover:

  • large-paste placeholder bookkeeping
  • local image attach/remove
  • shortcut image paste
  • attachment relabeling and related draft state

So the smallest coherent feature is “undo last paste transaction” at the composer layer.

Suggested implementation shape

  • capture a full draft snapshot before a successful paste
  • capture a post-paste fingerprint after it
  • on Ctrl+_ / alias:
    • if the current draft still matches the post-paste fingerprint, restore the pre-paste snapshot
    • otherwise treat the undo-paste state as stale and do nothing

This keeps the feature predictable and avoids introducing a broad edit-history system.

Test cases

  • undo after plain text paste
  • undo after oversized paste with placeholder + pending_pastes
  • undo after image-path paste that attached a local image
  • undo after shortcut image paste
  • cursor movement does not block undo
  • content edits after paste invalidate undo
  • one-shot behavior
  • Ctrl+Z remains suspend, not undo

Rationale / prior art

This key choice follows established terminal/editor conventions:

  • Emacs documents undo on C-/, with C-_ as the terminal-friendly equivalent on systems where Ctrl+/ is delivered that way.
  • The current Codex TUI already uses readline/emacs-style editing keys (Ctrl+U, Ctrl+K, Ctrl+W, Ctrl+Y), so a narrow paste undo on the same family of bindings is a reasonable fit.
  • Ctrl+Z should not be repurposed because it is already used for terminal suspend in the TUI.

Local status

I also have a local implementation of this behavior in a fork, verified with:

  • just fmt
  • cargo clippy -p codex-tui --tests
  • cargo test -p codex-tui

Reference implementation:

8d0fdd5f

If this direction matches the intended UX, I can turn it into a PR if invited.

Metadata

Metadata

Assignees

No one assigned

    Labels

    TUIIssues related to the terminal user interface: text input, menus and dialogs, and terminal displayenhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions