From 61f727fb96fb69b1f1dd8374b6f146b753af5dd4 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Mon, 4 Jan 2021 19:37:34 -0600 Subject: [PATCH 1/2] refactor all history modifications into discrete plugins --- clean_files.txt | 6 ++++++ lib/history.bash | 12 ------------ plugins/available/history-search.plugin.bash | 9 +++++++++ .../history-substring-search.plugin.bash | 9 +++++++++ plugins/available/history.plugin.bash | 18 ++++++++++-------- 5 files changed, 34 insertions(+), 20 deletions(-) delete mode 100644 lib/history.bash create mode 100644 plugins/available/history-search.plugin.bash create mode 100644 plugins/available/history-substring-search.plugin.bash diff --git a/clean_files.txt b/clean_files.txt index 6882ceff0d..f94b2dcafe 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -24,6 +24,12 @@ hooks .gitattributes lint_clean_files.sh +# plugins +# +plugins/available/history.plugin.bash +plugins/available/history-search.plugin.bash +plugins/available/history-substring-search.plugin.bash + # themes # themes/agnoster diff --git a/lib/history.bash b/lib/history.bash deleted file mode 100644 index a1a7cb04aa..0000000000 --- a/lib/history.bash +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -# Bash History Handling - -shopt -s histappend # append to bash_history if Terminal.app quits -export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups} # erase duplicates; alternative option: export HISTCONTROL=ignoredups -export HISTSIZE=${HISTSIZE:-5000} # resize history size -export AUTOFEATURE=${AUTOFEATURE:-true autotest} # Cucumber / Autotest integration - -function rh { - history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head -} diff --git a/plugins/available/history-search.plugin.bash b/plugins/available/history-search.plugin.bash new file mode 100644 index 0000000000..fd27585ce1 --- /dev/null +++ b/plugins/available/history-search.plugin.bash @@ -0,0 +1,9 @@ +cite about-plugin +about-plugin 'search history using the prefix already entered' + +# enter a few characters and press UpArrow/DownArrow +# to search backwards/forwards through the history +if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then + bind '"\e[A":history-search-backward' + bind '"\e[B":history-search-forward' +fi diff --git a/plugins/available/history-substring-search.plugin.bash b/plugins/available/history-substring-search.plugin.bash new file mode 100644 index 0000000000..6024b5247c --- /dev/null +++ b/plugins/available/history-substring-search.plugin.bash @@ -0,0 +1,9 @@ +cite about-plugin +about-plugin 'search history using the substring already entered' + +# enter a few characters and press UpArrow/DownArrow +# to search backwards/forwards through the history +if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then + bind '"\e[A":history-substring-search-backward' + bind '"\e[B":history-substring-search-forward' +fi diff --git a/plugins/available/history.plugin.bash b/plugins/available/history.plugin.bash index 528548ef02..b7029d272e 100644 --- a/plugins/available/history.plugin.bash +++ b/plugins/available/history.plugin.bash @@ -1,9 +1,11 @@ cite about-plugin -about-plugin 'history manipulation' -# enter a few characters and press UpArrow/DownArrow -# to search backwards/forwards through the history -if [[ ${SHELLOPTS} =~ (vi|emacs) ]] -then - bind '"":history-search-backward' - bind '"":history-search-forward' -fi +about-plugin 'improve history handling with sane defaults' + +# append to bash_history if Terminal.app quits +shopt -s histappend + +# erase duplicates; alternative option: export HISTCONTROL=ignoredups +export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups} + +# resize history to 100x the default (500) +export HISTSIZE=${HISTSIZE:-50000} From 4044f4123ca1a97497cae17a6b96a6ffed909005 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 15 Jan 2021 11:49:58 -0600 Subject: [PATCH 2/2] add top-history --- plugins/available/history-search.plugin.bash | 1 + .../history-substring-search.plugin.bash | 1 + plugins/available/history.plugin.bash | 28 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/plugins/available/history-search.plugin.bash b/plugins/available/history-search.plugin.bash index fd27585ce1..ea02eb7494 100644 --- a/plugins/available/history-search.plugin.bash +++ b/plugins/available/history-search.plugin.bash @@ -1,3 +1,4 @@ +# shellcheck shell=bash cite about-plugin about-plugin 'search history using the prefix already entered' diff --git a/plugins/available/history-substring-search.plugin.bash b/plugins/available/history-substring-search.plugin.bash index 6024b5247c..e0e37f43d5 100644 --- a/plugins/available/history-substring-search.plugin.bash +++ b/plugins/available/history-substring-search.plugin.bash @@ -1,3 +1,4 @@ +# shellcheck shell=bash cite about-plugin about-plugin 'search history using the substring already entered' diff --git a/plugins/available/history.plugin.bash b/plugins/available/history.plugin.bash index b7029d272e..08ca8de634 100644 --- a/plugins/available/history.plugin.bash +++ b/plugins/available/history.plugin.bash @@ -1,3 +1,4 @@ +# shellcheck shell=bash cite about-plugin about-plugin 'improve history handling with sane defaults' @@ -9,3 +10,30 @@ export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups} # resize history to 100x the default (500) export HISTSIZE=${HISTSIZE:-50000} + +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 \ + | awk '{ + a[$2]++ + }END{ + for(i in a) + printf("%s\t%s\n", a[i], i) + }' \ + | sort --reverse --numeric-sort \ + | head \ + | column \ + --table \ + --table-columns 'Command Count,Command Name' \ + --output-separator ' | ' +}