Skip to content

Commit 253004a

Browse files
authored
Merge pull request #1940 from gaelicWizard/history
Feature: automatic history management
2 parents 23f9b74 + 5d58580 commit 253004a

File tree

7 files changed

+65
-19
lines changed

7 files changed

+65
-19
lines changed

clean_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ completion/available/wpscan.completion.bash
8383
# libraries
8484
lib/colors.bash
8585
lib/helpers.bash
86+
lib/history.bash
8687
lib/log.bash
8788
lib/preexec.bash
8889
lib/search.bash

lib/history.bash

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# shellcheck shell=bash
2+
#
3+
# Functions for working with Bash's command history.
4+
5+
function _bash-it-history-init() {
6+
safe_append_preexec '_bash-it-history-auto-save'
7+
safe_append_prompt_command '_bash-it-history-auto-load'
8+
}
9+
10+
function _bash-it-history-auto-save() {
11+
case $HISTCONTROL in
12+
*'noauto'* | *'autoload'*)
13+
: # Do nothing, as configured.
14+
;;
15+
*'auto'*)
16+
# Append new history from this session to the $HISTFILE
17+
history -a
18+
;;
19+
*)
20+
# Append *only* if shell option `histappend` has been enabled.
21+
shopt -q histappend && history -a && return
22+
;;
23+
esac
24+
}
25+
26+
function _bash-it-history-auto-load() {
27+
case $HISTCONTROL in
28+
*'noauto'*)
29+
: # Do nothing, as configured.
30+
;;
31+
*'autosave'*)
32+
# Append new history from this session to the $HISTFILE
33+
history -a
34+
;;
35+
*'autoloadnew'*)
36+
# Read new entries from $HISTFILE
37+
history -n
38+
;;
39+
*'auto'*)
40+
# Blank in-memory history, then read entire $HISTFILE fresh from disk.
41+
history -a && history -c && history -r
42+
;;
43+
*)
44+
: # Do nothing, default.
45+
;;
46+
esac
47+
}
48+
49+
_bash_it_library_finalize_hook+=('_bash-it-history-init')
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# shellcheck shell=bash
22
about-plugin 'eternal bash history'
33

4-
# Load after the history plugin
5-
# BASH_IT_LOAD_PRIORITY: 375
4+
if [[ ${BASH_VERSINFO[0]} -lt 4 ]] || [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 3 ]]; then
5+
_log_warning "Bash version 4.3 introduced the 'unlimited' history size capability."
6+
return 1
7+
fi
68

79
# Modify history sizes before changing location to avoid unintentionally
810
# truncating the history file early.
911

1012
# "Numeric values less than zero result in every command being saved on the history list (there is no limit)"
11-
export HISTSIZE=-1
13+
readonly HISTSIZE=-1 2> /dev/null || true
1214

1315
# "Non-numeric values and numeric values less than zero inhibit truncation"
14-
export HISTFILESIZE='unlimited'
16+
readonly HISTFILESIZE='unlimited' 2> /dev/null || true
1517

1618
# Use a custom history file location so history is not truncated
1719
# if the environment ever loses this "eternal" configuration.
1820
HISTDIR="${XDG_STATE_HOME:-${HOME?}/.local/state}/bash"
1921
[[ -d ${HISTDIR?} ]] || mkdir -p "${HISTDIR?}"
20-
export HISTFILE="${HISTDIR?}/history"
22+
readonly HISTFILE="${HISTDIR?}/history" 2> /dev/null || true

plugins/available/history-search.plugin.bash

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# shellcheck shell=bash
22
about-plugin 'search history using the prefix already entered'
33

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

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# shellcheck shell=bash
22
about-plugin 'search history using the substring already entered'
33

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

plugins/available/history.plugin.bash

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ about-plugin 'improve history handling with sane defaults'
55
# variable when the shell exits, rather than overwriting the file.
66
shopt -s histappend
77

8-
# erase duplicates; alternative option: export HISTCONTROL=ignoredups
9-
export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups}
8+
# 'ignorespace': don't save command lines which begin with a space to history
9+
# 'erasedups' (alternative 'ignoredups'): don't save duplicates to history
10+
# 'autoshare': automatically share history between multiple running shells
11+
: "${HISTCONTROL:=ignorespace:erasedups:autoshare}"
1012

1113
# resize history to 100x the default (500)
12-
export HISTSIZE=${HISTSIZE:-50000}
13-
14-
# Flush history to disk after each command.
15-
export PROMPT_COMMAND="history -a;${PROMPT_COMMAND}"
14+
: "${HISTSIZE:=50000}"
1615

1716
function top-history() {
1817
about 'print the name and count of the most commonly run tools'

themes/base.theme.bash

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ function aws_profile {
584584
}
585585

586586
function _save-and-reload-history() {
587-
local autosave=${1:-0}
588-
[[ $autosave -eq 1 ]] && history -a && history -c && history -r
587+
local autosave="${1:-${HISTORY_AUTOSAVE:-0}}"
588+
[[ ${autosave} -eq 1 ]] && local HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autoshare"
589+
_bash-it-history-auto-save && _bash-it-history-auto-load
589590
}

0 commit comments

Comments
 (0)