Skip to content

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

Backlog

Description

@abs-zer

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

Activity

  1. added
    type-bug
    on Dec 5, 2026
  2. terryjreedy commented on Dec 5, 2026

    @terryjreedy
    Member

    I confirmed this on Windows 3.15.

  3. mkhlz commented on Dec 5, 2026

    @mkhlz

    Reproduced this in the IDLE Editor on Windows 10 (Python 3.15.2, Tk 9.0.4) using a small script driving EditorWindow (I haven't tested IDLE Shell, macOS, Linux, Cut, or a build of main).

    What I observed

    Starting with "foo bar" and "foo" selected:

    • Paste over selection (EditorWindow.paste) and type over selection (tk::TextInsert) each create 2 undo entries (win.undo.pointer == 2). First undo leaves " bar", second undo restores "foo bar", and redo also takes 2 steps.
    • Plain deletion (text.delete("sel.first", "sel.last")) creates 1 undo entry.
    • In all three cases, undoing does not restore the sel tag.

    Here is a minimal snippet showing the two-step undo for typing over a selection:

    import tkinter
    from idlelib.editor import EditorWindow
    
    root = tkinter.Tk()
    root.withdraw()
    win = EditorWindow(root=root)
    text = win.text
    
    text.insert("1.0", "foo bar")
    win.undo.reset_undo()
    text.tag_add("sel", "1.0", "1.3")
    text.mark_set("insert", "1.3")
    
    text.tk.eval("auto_load tk::TextInsert")
    text.tk.call("tk::TextInsert", str(text), "X")
    print("undo entries:", win.undo.pointer)  # 2
    
    win._close()
    root.destroy()

    Why changing paste() alone won't cover typing

    Typing over a selection does not go through EditorWindow.paste(). Tk's tk::TextInsert deletes the selection and inserts the new character using Tk's own undo separators, which IDLE's UndoDelegator does not use.

    As a quick probe, wrapping either edit from outside with text.undo_block_start() and text.undo_block_stop() gave one-step undo and redo, though the selection was still not restored.

    Next steps / question

    Grouping the delete + insert into one undo block and restoring the sel range on undo appear to be separate pieces (the latter also affects plain deletion). Would a maintainer prefer those as one PR or two?

    I'd be happy to work on a PR for this if that sounds good.

    Note: I used some AI assistance to investigate the idlelib code and write the reproduction script; otherwise, I ran the reproduction locally and verified the results myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-IDLEtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions