diff --git a/clean_files.txt b/clean_files.txt index 50e8a7a1..53765a5f 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -92,6 +92,7 @@ plugins/available/docker-machine.plugin.bash plugins/available/git.plugin.bash plugins/available/go.plugin.bash plugins/available/goenv.plugin.bash +plugins/available/history-eternal.plugin.bash plugins/available/history-search.plugin.bash plugins/available/history-substring-search.plugin.bash plugins/available/history.plugin.bash diff --git a/plugins/available/history-eternal.plugin.bash b/plugins/available/history-eternal.plugin.bash new file mode 100644 index 00000000..a18283d3 --- /dev/null +++ b/plugins/available/history-eternal.plugin.bash @@ -0,0 +1,20 @@ +# shellcheck shell=bash +about-plugin 'eternal bash history' + +# Load after the history plugin +# BASH_IT_LOAD_PRIORITY: 375 + +# Modify history sizes before changing location to avoid unintentionally +# truncating the history file early. + +# "Numeric values less than zero result in every command being saved on the history list (there is no limit)" +export HISTSIZE=-1 + +# "Non-numeric values and numeric values less than zero inhibit truncation" +export HISTFILESIZE='unlimited' + +# Use a custom history file location so history is not truncated +# if the environment ever loses this "eternal" configuration. +HISTDIR="${XDG_STATE_HOME:-${HOME?}/.local/state}/bash" +[[ -d ${HISTDIR?} ]] || mkdir -p "${HISTDIR?}" +export HISTFILE="${HISTDIR?}/history" diff --git a/plugins/available/history-search.plugin.bash b/plugins/available/history-search.plugin.bash index ea02eb74..341ce2af 100644 --- a/plugins/available/history-search.plugin.bash +++ b/plugins/available/history-search.plugin.bash @@ -1,7 +1,9 @@ # shellcheck shell=bash -cite about-plugin about-plugin 'search history using the prefix already entered' +# Load after the history plugin +# BASH_IT_LOAD_PRIORITY: 375 + # enter a few characters and press UpArrow/DownArrow # to search backwards/forwards through the history if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then diff --git a/plugins/available/history-substring-search.plugin.bash b/plugins/available/history-substring-search.plugin.bash index e0e37f43..586ceb50 100644 --- a/plugins/available/history-substring-search.plugin.bash +++ b/plugins/available/history-substring-search.plugin.bash @@ -1,7 +1,9 @@ # shellcheck shell=bash -cite about-plugin about-plugin 'search history using the substring already entered' +# Load after the history plugin +# BASH_IT_LOAD_PRIORITY: 375 + # enter a few characters and press UpArrow/DownArrow # to search backwards/forwards through the history if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then diff --git a/plugins/available/history.plugin.bash b/plugins/available/history.plugin.bash index 08ca8de6..be253e4a 100644 --- a/plugins/available/history.plugin.bash +++ b/plugins/available/history.plugin.bash @@ -1,8 +1,8 @@ # shellcheck shell=bash -cite about-plugin about-plugin 'improve history handling with sane defaults' -# append to bash_history if Terminal.app quits +# Append the history list to the file named by the value of the HISTFILE +# variable when the shell exits, rather than overwriting the file. shopt -s histappend # erase duplicates; alternative option: export HISTCONTROL=ignoredups @@ -11,19 +11,17 @@ export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups} # resize history to 100x the default (500) export HISTSIZE=${HISTSIZE:-50000} -top-history() { +# Flush history to disk after each command. +export PROMPT_COMMAND="history -a;${PROMPT_COMMAND}" + +function top-history() { about 'print the name and count of the most commonly run tools' - if [[ -n $HISTTIMEFORMAT ]]; then - # To parse history we need a predictable format, which HISTTIMEFORMAT - # gets in the way of. So we unset it and set a trap to guarantee the - # user's environment returns to normal even if the pipeline below fails. - # shellcheck disable=SC2064 - trap "export HISTTIMEFORMAT='$HISTTIMEFORMAT'" RETURN - unset HISTTIMEFORMAT - fi - - history \ + # - Make sure formatting doesn't interfer with our parsing + # - Use awk to count how many times the first command on each line has been called + # - Truncate to 10 lines + # - Print in column format + HISTTIMEFORMAT='' history \ | awk '{ a[$2]++ }END{