Skip to content

Instantly share code, notes, and snippets.

@chugcup
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save chugcup/29c76d7ccecd53aefded to your computer and use it in GitHub Desktop.

Select an option

Save chugcup/29c76d7ccecd53aefded to your computer and use it in GitHub Desktop.
Personal .pythonrc
#################################################################
# Add the following to your bashrc to enable loading this script
# alias py='python -i $HOME/.pythonrc'
#################################################################
import os, sys
from pprint import pprint
home=os.path.expanduser('~')
history_file=home+'/.python_history'
if not os.path.exists(history_file): open(history_file, 'w').close()
cwd = os.getcwd()
if cwd not in sys.path: sys.path.append(cwd)
del os
del sys
# this add completion to python interpreter
import readline
import rlcompleter
readline.parse_and_bind("set show-all-if-ambiguous on")
readline.parse_and_bind("tab: complete")
readline.read_history_file(history_file)
readline.set_history_length(-1)
# this cleans the history of the interpreter and save it to file
def clean_history(histfile):
L=readline.get_current_history_length()
history=[]
for i in range(1,L+1): history.append(readline.get_history_item(i))
history.reverse()
clean_history=[]
for i in range(len(history)):
command=history[i]
if command not in clean_history:
clean_history.append(command)
else:
readline.remove_history_item(len(history)-(i+1))
readline.write_history_file(histfile)
# here we register the "clean_history" function
import atexit
atexit.register(clean_history,history_file)
@chugcup

chugcup commented May 12, 2014

Copy link
Copy Markdown
Author

Adds tab completion and command history to Python interpreter

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