Skip to content

Instantly share code, notes, and snippets.

@benhodgson
Created May 4, 2011 12:41
Show Gist options
  • Save benhodgson/955154 to your computer and use it in GitHub Desktop.
Save benhodgson/955154 to your computer and use it in GitHub Desktop.
Add history between sessions and auto-completion via the ESC and tab keys to the interactive Python interpreter with this dot-file.
"""
Python Startup File (from https://gist.github.com/955154)
Add this file to ~/.pythonrc.py to add history between sessions and
auto-completion via the ESC key to the interactive Python interpreter. After
adding this file, put something like the following line in your .bash_profile:
export PYTHONSTARTUP=$HOME/.pythonrc.py
Requires a recent version of Python and the readline package, which you can
install from http://pypi.python.org/pypi/readline or with easy_install.
easy_install -U readline
"""
def _pythonrc():
import atexit
import os
import readline
import rlcompleter
readline.parse_and_bind("tab: complete")
history = os.path.expanduser("~/.py_history")
readline.set_history_length(500)
if os.path.exists(history):
readline.read_history_file(history)
@atexit.register
def write_history(history=history):
readline.write_history_file(history)
_pythonrc()
del _pythonrc
@zacharyvoase
Copy link

Tip: atexit.register(readline.write_history_file, history)

@ldong
Copy link

ldong commented Apr 10, 2014

Cool pythonrc file. Thank you.

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