Configuring history

In yesterday’s post the past repo was described. The first step to getting that to work is to correctly configure history files in the first place. Some are easy, but some are more complex.

For zsh and MySQL it’s rather easy. Just put something like this in your ~/.zshrc:

setopt append_history hist_allow_clobber hist_ignore_dups
setopt hist_ignore_space hist_no_store hist_reduce_blanks
setopt hist_verify inc_append_history
if [[ ! -d ~/.history.d ]]; then
  mkdir ~/.history.d
export HISTFILE=~/.history.d/${HOST%%.*}
export MYSQL_HISTFILE=~/.history.d/${HOST%%.*}.mysql

For [bash][bash] it’s similar:

shopt -s histappend
if [ ! -d "$HOME/.history.d" ]; then
  mkdir "$HOME/.history.d"
export HISTFILE=$HOME/.history.d/${HOSTNAME%%.*}.bash

In both bash and zsh I use a setting that prevents commands prefixed with a space from being saved to history. I do this when I run a command with a password on them. MySQL also blocks some statements from being stored.

Lastly is Python which proved more difficult due to differences between python on Linux and on OS X. I haven’t yet tested it with python 3.x versions.

# vim:ft=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 tab key - see the readline.parse_and_bind call.
# Store the file in ~/.pystartup, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.
# Based off the 2.4 python docs, changed by

import atexit
import os
import sys
import readline
import rlcompleter

# Set readline interactive options.
if 'libedit' in readline.__doc__:
  # Fucking Apple.
  readline.parse_and_bind("bind ^I rl_complete")
  readline.parse_and_bind('tab: complete')

# Set history path (shard by version), saving and recall.
historyPath = os.path.expanduser("~/.history.d/%s.python%s.%s.interactive"
    % (os.uname()[1].split('.')[0], sys.version_info[0], sys.version_info[1]))

def save_history(historyPath=historyPath):
  import readline

if os.path.exists(historyPath):

print('TODO: Define some funcs to import history from other versions.')


# Cleanup.
del os, sys, atexit, readline, rlcompleter, save_history, historyPath