dotfiles/python3_startup.py
Dan Buch a8cd5a2fe9 Attempting to carve out a sensible chunk of stuff for a dotfiles repo.
Sure hope I didn't miss any super secret crap :-P
2012-02-26 14:38:01 -05:00

52 lines
1.6 KiB
Python

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it, e.g. "export PYTHONSTARTUP=/max/home/itamar/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.
import atexit
import os
import readline
import rlcompleter
history_path = os.path.expanduser("~/.pyhistory")
history_tmp = os.path.expanduser("~/.pyhisttmp.py")
end_marker= "# # # histDUMP # # #"
save_macro = """\
import readline
readline.write_history_file('""" + history_tmp + """')
print '####>>>>>>>>>>'
print ''.join(list(filter(
lambda line_part: not line_part.strip().endswith('""" + end_marker +"""'), \
open('""" + history_tmp + """').readlines())[:])) + '####<<<<<<<<<<'""" + end_marker
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('\C-w: "' + save_macro + '"')
def save_history(history_path=history_path, end_marker=end_marker):
import readline
readline.write_history_file(history_path)
lines = list(filter(lambda line_part, end_marker=end_marker:
not line_part.strip().endswith(end_marker), open(history_path).readlines()))
open(history_path, 'w+').write(''.join(lines))
if os.path.exists(history_path):
readline.read_history_file(history_path)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, history_path
del history_tmp, end_marker, save_macro