Brain Phrye

code cooking diy fiction personal photos politics reviews tools


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:

1
2
3
4
5
6
7
8
9
10
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
HISTSIZE=5000
SAVEHIST=5000
if [[ ! -d ~/.history.d ]]; then
mkdir ~/.history.d
fi
export HISTFILE=~/.history.d/${HOST%%.*}
export MYSQL_HISTFILE=~/.history.d/${HOST%%.*}.mysql

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

1
2
3
4
5
6
7
8
HISTCONTROL=ignoreboth
shopt -s histappend
HISTSIZE=1000
HISTFILESIZE=2000
if [ ! -d "$HOME/.history.d" ]; then
mkdir "$HOME/.history.d"
fi
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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 kevin@ie.suberic.net
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")
else:
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
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
print('TODO: Define some funcs to import history from other versions.')
atexit.register(save_history)
# Cleanup.
del os, sys, atexit, readline, rlcompleter, save_history, historyPath