-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotbash_hist
106 lines (86 loc) · 2.33 KB
/
dotbash_hist
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
#
# .bashrc_history
#
# Bash helpers for processing shell history
#
HISTDIR=$HOME/.cache/shell
if [ ! -d ${HISTDIR} ]; then
mkdir -p ${HISTDIR}
fi
# append (as I use lots of shells)
shopt -s histappend
export HISTFILE=$HISTDIR/bash_${BASHPID}_history
export ALL_HISTORY=$HISTDIR/all_bash_history
export HISTCONTROL=ignoreboth:erasedups
export HISTIGNORE="&:rm *ls:[bf]g:exit:mpv"
export HISTSIZE=100000
export HISTFILESIZE=${HISTSIZE}
export PROMPT_COMMAND="history -a"
# Detect HH and set it up if we have it
HAVE_HH=$(find_alternatives "hh")
if [ -e ${HAVE_HH} ]; then
export HH_CONFIG=keywords,hicolor
HISTMSG="with hh"
# if this is interactive shell, then bind hh to Ctrl-r
if [[ $- =~ .*i.* ]]; then
bind '"\C-r": "\C-a hh \C-j"'
fi
else
HISTMSG="plain"
fi
function clean_history
{
cleaned=0
pids=$(ps -C bash -o pid --no-headers)
for f in $HISTDIR/bash*_history; do
fpid=`echo $f | sed -r 's/.*_([0-9]*)_.*/\1/g'`
# is it still running?
for p in $pids; do
if test $p -eq $fpid; then
f=""
break;
fi
done;
if [ ! -z "$f" ]; then
let cleaned+=1
rm $f
fi
done
echo "Cleaned $cleaned stale entries"
}
function combine_history
{
(cd $HISTDIR;
tmp_hist=$(mktemp /tmp/hist.XXXXXX);
awk '{ if (length($0) < 2048) print }' ${ALL_HISTORY} bash_* | grep -v "^(cd|copy_|sleep|rm|ls|mv|mpv|mkv2mp4)" > $tmp_hist
sort -u $tmp_hist > ${ALL_HISTORY};
rm $tmp_hist)
}
# Build up combined history and then remove current HISTFILE
# intended to be called on exiting the shell
function flush_history
{
builtin history -a # append current history to $HISTFILE
cat ${HISTFILE} >> ${ALL_HISTORY}
rm ${HISTFILE}
}
# Flush history on exit
trap "{ flush_history; }" EXIT TERM
function h
{
builtin history -a
combine_history
if [ -e ${HAVE_HH} ]; then
HISTFILE=${ALL_HISTORY} hh "$@"
else
builtin history | ${GREP} "$@"
fi
}
# Now setup history for this shell, we shall combine history
# and reload it into this file
combine_history
cp ${ALL_HISTORY} ${HISTFILE}
HISTMSG="${HISTMSG}, $(cat ${HISTFILE} | wc -l) entries"
alias .hist="source ${DOTFILES_DIR}/dotbash_hist"
echo "loading .bash_hist ($HISTMSG)"