Permalink
Browse files

Have python save and load a ~/.python_history

Similar to how bash uses .bash_history.  Nice!
  • Loading branch information...
benley committed Mar 18, 2012
1 parent a6db59b commit 86b0e7a3ebbab3b152f76b3a8124cdf6e9f96cbb
Showing with 10 additions and 1 deletion.
  1. +10 −1 .pythonrc
View
@@ -1,9 +1,9 @@
# To use this, put this in your .bashrc or other shell login file:
# export PYTHONSTARTUP="${HOME}/.pythonrc"
+
# This makes tab-completion work in interactive python interpreters, as long
# as they are compiled with gnu readline support. If not, python will still
# work, just without tab completion.
-
try:
import readline
except ImportError:
@@ -14,3 +14,12 @@ else:
readline.parse_and_bind("tab: complete")
# And this works on osx. Weird.
readline.parse_and_bind("bind ^i rl_complete")
+
+# Restore our command-line history, and save it when Python exits.
+# Similar to how bash does it with .bash_history.
+import atexit
+import os.path
+history_path = os.path.expanduser('~/.python_history')
+if os.path.isfile(history_path):
+ readline.read_history_file(history_path)
+atexit.register(lambda x=history_path: readline.write_history_file(x))

0 comments on commit 86b0e7a

Please sign in to comment.