Skip to content

Commit bdb5289

Browse files
committed
Wholesale cleanup of all history plugins
- remove the use of redundant cite - minor cleanups to history.plugin.bash - set explicit load orders for history-search and history-substring-search - add new plugin history-eternal - replace superfluous trap for managing HISTTIMEFORMAT changes
1 parent 036cf8a commit bdb5289

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

clean_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ plugins/available/docker-machine.plugin.bash
9292
plugins/available/git.plugin.bash
9393
plugins/available/go.plugin.bash
9494
plugins/available/goenv.plugin.bash
95+
plugins/available/history-eternal.plugin.bash
9596
plugins/available/history-search.plugin.bash
9697
plugins/available/history-substring-search.plugin.bash
9798
plugins/available/history.plugin.bash
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# shellcheck shell=bash
2+
about-plugin 'eternal bash history'
3+
4+
# Load after the history plugin
5+
# BASH_IT_LOAD_PRIORITY: 375
6+
7+
# Modify history sizes before changing location to avoid unintentionally
8+
# truncating the history file early.
9+
10+
# "Numeric values less than zero result in every command being saved on the history list (there is no limit)"
11+
export HISTSIZE=-1
12+
13+
# "Non-numeric values and numeric values less than zero inhibit truncation"
14+
export HISTFILESIZE='unlimited'
15+
16+
# Use a custom history file location so history is not truncated
17+
# if the environment ever loses this "eternal" configuration.
18+
HISTDIR="${XDG_STATE_HOME:-${HOME?}/.local/state}/bash"
19+
[[ -d ${HISTDIR?} ]] || mkdir -p "${HISTDIR?}"
20+
export HISTFILE="${HISTDIR?}/history"

plugins/available/history-search.plugin.bash

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# shellcheck shell=bash
2-
cite about-plugin
32
about-plugin 'search history using the prefix already entered'
43

4+
# Load after the history plugin
5+
# BASH_IT_LOAD_PRIORITY: 375
6+
57
# enter a few characters and press UpArrow/DownArrow
68
# to search backwards/forwards through the history
79
if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then

plugins/available/history-substring-search.plugin.bash

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# shellcheck shell=bash
2-
cite about-plugin
32
about-plugin 'search history using the substring already entered'
43

4+
# Load after the history plugin
5+
# BASH_IT_LOAD_PRIORITY: 375
6+
57
# enter a few characters and press UpArrow/DownArrow
68
# to search backwards/forwards through the history
79
if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then

plugins/available/history.plugin.bash

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# shellcheck shell=bash
2-
cite about-plugin
32
about-plugin 'improve history handling with sane defaults'
43

5-
# append to bash_history if Terminal.app quits
4+
# Append the history list to the file named by the value of the HISTFILE
5+
# variable when the shell exits, rather than overwriting the file.
66
shopt -s histappend
77

88
# erase duplicates; alternative option: export HISTCONTROL=ignoredups
@@ -11,19 +11,17 @@ export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups}
1111
# resize history to 100x the default (500)
1212
export HISTSIZE=${HISTSIZE:-50000}
1313

14-
top-history() {
15-
about 'print the name and count of the most commonly run tools'
14+
# Flush history to disk after each command.
15+
export PROMPT_COMMAND="history -a;${PROMPT_COMMAND}"
1616

17-
if [[ -n $HISTTIMEFORMAT ]]; then
18-
# To parse history we need a predictable format, which HISTTIMEFORMAT
19-
# gets in the way of. So we unset it and set a trap to guarantee the
20-
# user's environment returns to normal even if the pipeline below fails.
21-
# shellcheck disable=SC2064
22-
trap "export HISTTIMEFORMAT='$HISTTIMEFORMAT'" RETURN
23-
unset HISTTIMEFORMAT
24-
fi
17+
function top-history() {
18+
about 'print the name and count of the most commonly run tools'
2519

26-
history \
20+
# - Make sure formatting doesn't interfer with our parsing
21+
# - Use awk to count how many times the first command on each line has been called
22+
# - Truncate to 10 lines
23+
# - Print in column format
24+
HISTTIMEFORMAT='' history \
2725
| awk '{
2826
a[$2]++
2927
}END{

0 commit comments

Comments
 (0)