Repository navigation
Description
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
- Open IDLE Shell or an IDLE Editor window.
- Type
foo barand then selectfoo. - While
foois highlighted, type an X. Or, copysnackinto the [system] clipboard and then typeCtrl+Vto paste overworld. - Type
Ctrl+Z(or Edit:Undo). - Type
Ctrl+Z(or Edit:Undo) again. - Type
Ctrl+Shift+Z(or Edit:Redo). - Type
Ctrl+Shift+Z(or Edit:Redo) again.
Actual behavior
- The text is now
X barorsnack bar(as expected). - First Undo: Only removes the typed or pasted text (
xorsnack), leavingbar(with the space). - Second Undo: Restores
foo, leavingfoo bar(delayed success). Does not restore the selection highlight onfoo(incomplete Undo). Repeating the typing or pasting would not have the same effect because the selection has been lost or discarded. - First Redo: Redoes the delete part.
- 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 theselrange so the previously selected text is highlighted again.
6 Single-step Redo: The firstCtrl+Zshould 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
- Undo grouping (
Lib/idlelib/editor.py):EditorWindow.paste()generates<<Paste>>without wrapping it inundo_block_start()/undo_block_stop(), so Tk's underlyingdeleteandinsertoperations are recorded as two separate commands onUndoDelegator: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"
- Selection restoration (
Lib/idlelib/undo.py):DeleteCommand.undo()reinsertsself.charsatself.index1without re-applying theseltag across(self.index1, self.index2)for multi-character deletions.
CPython versions tested on:
3.15
Operating systems tested on:
Windows
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
terryjreedy commentedon Dec 5, 2026
I confirmed this on Windows 3.15.
mkhlz commentedon Dec 5, 2026
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 ofmain).What I observed
Starting with
"foo bar"and"foo"selected: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.text.delete("sel.first", "sel.last")) creates 1 undo entry.seltag.Here is a minimal snippet showing the two-step undo for typing over a selection:
Why changing
paste()alone won't cover typingTyping over a selection does not go through
EditorWindow.paste(). Tk'stk::TextInsertdeletes the selection and inserts the new character using Tk's own undo separators, which IDLE'sUndoDelegatordoes not use.As a quick probe, wrapping either edit from outside with
text.undo_block_start()andtext.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
selrange 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
idlelibcode and write the reproduction script; otherwise, I ran the reproduction locally and verified the results myself.