Skip to content

Commit 8c9406d

Browse files
committed
lib/history: new functions _bash_it_history_auto_*()
Two new functions `_bash_it_history_auto_save()` and `_bash_it_history_auto_load()`, which append new history to disk and load new history from disk, respectively. See #1595 for discussion.
1 parent dec0d15 commit 8c9406d

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

clean_files.txt

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ completion/available/vuejs.completion.bash
7777
completion/available/wpscan.completion.bash
7878

7979
# libraries
80+
lib/history.bash
8081
lib/utilities.bash
8182

8283
# plugins

lib/history.bash

+49
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_auto_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_history_auto_init

plugins/available/history.plugin.bash

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ 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: HISTCONTROL=ignoredups
9-
: "${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)
1214
: "${HISTSIZE:=50000}"
1315

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

themes/base.theme.bash

+2-2
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,6 @@ function safe_append_prompt_command {
620620
}
621621

622622
function _save-and-reload-history() {
623-
local autosave=${1:-0}
624-
[[ $autosave -eq 1 ]] && history -a && history -c && history -r
623+
[[ ${1:-${autosave:-${HISTORY_AUTOSAVE:-0}}} -eq 1 ]] && local HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autoshare"
624+
_bash_it_history_auto_save && _bash_it_history_auto_load
625625
}

0 commit comments

Comments
 (0)