Unable to redo after undo
Author: bewCreated Jan 12, 2023Updated Jul 1, 2026
Hello!
While tweaking my ipython REPL keybindings, I discovered that buf.redo() never works.
Here is a little script that shows the issue:
#!/usr/bin/env python
from prompt_toolkit import PromptSession
from prompt_toolkit.key_binding import KeyBindings
def dyn_prompt(buf):
def inner():
return f"undo: {len(buf._undo_stack)} | redo: {len(buf._redo_stack)}"
return inner
def main():
kb = KeyBindings()
@kb.add("escape", "u")
def _(event):
event.current_buffer.undo()
@kb.add("escape", "r")
def _(event):
event.current_buffer.redo()
print("Try to type some text, then try: Alt-u to undo | Alt-r to redo")
s = PromptSession("> ", key_bindings=kb)
s.rprompt = dyn_prompt(s.app.current_buffer)
try:
s.prompt()
except (EOFError, KeyboardInterrupt):
pass
if __name__ == "__main__":
main()- Input
abcdef - RPrompt is now
undo: 6 | redo: 0 - Type
Alt-uto undo (removef) - RPrompt is now
undo: 5 | redo: 1 - Type
Alt-uto undo again (removee) - RPrompt is now
undo: 4 | redo: 1(redo didn't change!) - Type
Alt-rto try to redo (adde) - :warning:
eis not added, redo didn't work! - RPrompt is now
undo: 5 | redo: 0
Is that the right way to try to use undo/redo ? Is that a bug on your side maybe?
Source: prompt-toolkit/python-prompt-toolkit