4

A feature in bpython called rewind.

Is there some similar key bindings?

iMom0
  • 331

1 Answers1

6

Short answer: No, IPython does not have this feature.

However, based on my understanding of bpython's docs, their rewind isn't actually stepping backward, it's starting over and replaying to a point earlier in the session. If this is actually the case, then in IPython you can do something that might be similar by resetting and re-running the history:

def rewind(ip, s=''):
    """attempt to reset IPython to an earlier state

    implemented by resetting IPython, and replaying the
    history up to (but not including) the specified index.
    """
    if s:
        stop = min(int(s), ip.execution_count)
    else:
        # backup 1 by default
        stop = ip.execution_count-1
    # fetch the history
    hist = list(ip.history_manager.get_range(stop=stop))
    # reset IPython
    ip.reset()
    ip.execution_count=0
    # replay the history
    for _,i,cell in hist:
        ip.run_cell(cell, store_history=True)

# expose this function as %rewind
get_ipython().define_magic('rewind', rewind)

Then at a later point, call %rewind 6, and you should have IPython back in the state prior to input #6. This implementation is far from perfect, because I just tossed it together (for instance, it will not suppress tracebacks or output for the replayed cells), but it should be a start.

minrk
  • 198