← Winnitude! Search  ·  Reader Mode (1519 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 11 LINKS

IDLE: Pasting or typing over a selection requires two Undo steps to undo it. Undo does not restore selection. · Issue #163179 · python/cpython

Repository navigation

IDLE: Pasting or typing over a selection requires two Undo steps to undo it. Undo does not restore selection.  #163179


New issue

New issue

Backlog

Backlog
IDLE: Pasting or typing over a selection requires two Undo steps to undo it. Undo does not restore selection. #163179 Labels
topic-IDLE

type-bug

Description

@abs-zer's profile abs-zer opened on Dec 5, 2026

Bug report

Summary

In IDLE (both the Shell and Editor windows):
If I Paste (Ctrl+V) over or type over selected text (replacing it), undoing that one action takes two Undo (Ctrl+Z) steps. (The editor handles the paste-over or type-over as a delete followed by an insert, but it does not "group" the two sub-actions. Afterward, my first Undo undoes the insert and then my second Undo undoes the delete.) (Every other editing interface will undo a paste-over or a type-over in only one step.)
Also, undoing a deletion (or a paste or typing over selected text) fails to restore the previous text selection. (Restoring the prior selection is the norm, so that, for example, selecting text, overtyping X, Undoing, and then typing Y replaces the selected text with Y.)

Steps to reproduce

  1. Open IDLE Shell or an IDLE Editor window.
  2. Type foo bar and then select foo.
  3. While foo is highlighted, type an X. Or, copy snack into the [system] clipboard and then type Ctrl+V to paste over world.
  4. Type Ctrl+Z (or Edit:Undo).
  5. Type Ctrl+Z (or Edit:Undo) again.
  6. Type Ctrl+Shift+Z (or Edit:Redo).
  7. Type Ctrl+Shift+Z (or Edit:Redo) again.

Actual behavior

  1. The text is now X bar or snack bar (as expected).
  2. First Undo: Only removes the typed or pasted text (x or snack), leaving bar (with the space).
  3. Second Undo: Restores foo, leaving foo bar (delayed success). Does not restore the selection highlight on foo (incomplete Undo). Repeating the typing or pasting would not have the same effect because the selection has been lost or discarded.
  4. First Redo: Redoes the delete part.
  5. Second Undo: Redoes the typing or replacement.

Expected behavior

4 Single-step Undo: The first Ctrl+Z should have undone the paste-over (or type-over) as one step. (This allows users to symmetrically Undo and Redo in single steps, for whatever reason, including comparing before-and-after edits).

  • Selection restoration: Undoing a cut (Ctrl+X) or paste-over should restore the sel range so the previously selected text is highlighted again.
    6 Single-step Redo: The first Ctrl+Z should have undone the paste-over (or type-over) as one step. (This allows users to symmetrically Undo and Redo in single steps, for whatever reason, including comparing before-and-after edits).

Cause & suggested fix

  1. Undo grouping (Lib/idlelib/editor.py): EditorWindow.paste() generates <<Paste>> without wrapping it in undo_block_start() / undo_block_stop(), so Tk's underlying delete and insert operations are recorded as two separate commands on UndoDelegator:
    def paste(self, event=None):
        self.text.undo_block_start()
        try:
            self.text.event_generate("<<Paste>>")
        finally:
            self.text.undo_block_stop()
        self.text.see("insert")
        return "break"
  2. Selection restoration (Lib/idlelib/undo.py): DeleteCommand.undo() reinserts self.chars at self.index1 without re-applying the sel tag across (self.index1, self.index2) for multi-character deletions.

CPython versions tested on:

3.15

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels


    topic-IDLE

    type-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions