Skip to content

Instantly share code, notes, and snippets.

@Fusion
Created July 3, 2020 21:17
Show Gist options
  • Save Fusion/5b705b9cd1dc882db90b9628bae859e7 to your computer and use it in GitHub Desktop.
Save Fusion/5b705b9cd1dc882db90b9628bae859e7 to your computer and use it in GitHub Desktop.
Add history management to Python REPL #python
def history(numlines = -1):
import readline
total = readline.get_current_history_length()
if numlines == -1:
numlines = total
if numlines > 0:
for i in range(total - numlines + 1, total + 1):
print("%3d %s" % (i, readline.get_history_item(i)))
def bang(linenum = -1):
import readline
total = readline.get_current_history_length()
if linenum == -1:
linenum = total - 1
if linenum > 0:
r = readline.get_history_item(linenum)
exec(r)
readline.replace_history_item(total - 1, r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment