You can emulate the behavior of bash by adding this to one of the startup scripts (e.g. $(ipython locate profile)/startup/log_history.py :
import atexit import os ip = get_ipython() LIMIT = 1000 # limit the size of the history def save_history(): """save the IPython history to a plaintext file""" histfile = os.path.join(ip.profile_dir.location, "history.txt") print("Saving plaintext history to %s" % histfile) lines = [] # get previous lines # this is only necessary because we truncate the history, # otherwise we chould just open with mode='a' if os.path.exists(histfile): with open(histfile, 'r') as f: lines = f.readlines() # add any new lines from this session lines.extend(record[2] + '\n' for record in ip.history_manager.get_range()) with open(histfile, 'w') as f: # limit to LIMIT entries f.writelines(lines[-LIMIT:]) # do the save at exit atexit.register(save_history)
Note that this emulates the behavior of the bash / readline history in that it will fail if the interpreter fails, etc.
in fact
update: alternative
If what you really want is to have only a few hand-held favorite commands available for reading (completion, ^ search R, etc.) that you can control versions of, this boot file allows you to save this file is on its own, which is purely in addition to the actual history of IPython commands:
import os ip = get_ipython() favfile = "readline_favorites" def load_readline_favorites(): """load profile_dir/readline_favorites into the readline history""" path = os.path.join(ip.profile_dir.location, favfile) if not os.path.exists(path): return with open(path) as f: for line in f: ip.readline.add_history(line.rstrip('\n')) if ip.has_readline: load_readline_favorites()
Put this in your profile_default/startup/ directory and edit profile_default/readline_favorites , or anywhere else to save this file, and it will appear at the end of readline, etc. in every IPython session.
minrk
source share