From ff41d9acc805099f27f1cf060ca9db72d2fdb74f Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 30 Jan 2023 12:24:02 -0600 Subject: Bash! --- bash/history.bash | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'bash/history.bash') diff --git a/bash/history.bash b/bash/history.bash index 95edf9d..d2bdceb 100644 --- a/bash/history.bash +++ b/bash/history.bash @@ -1,5 +1,5 @@ # Bash history settings -# I don't export any variables in this file because history settings +# I don't export any variables in this file because history settings # really only apply in an interactive session. # XDG compliance @@ -33,3 +33,39 @@ HISTIGNORE="$HISTIGNORE:ls:exit:cd" # Automatically append to HISTFILE on every command PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-:}" + +## Make a new function from a history item: WIP +# In particular, the arguments of this function could be better imo +histfunc() { # histfunc NAME QUERY + local name="$1" + local query="$2" + local -a cands + while read -r cmd; do + cands=( "${cands[@]}" "$cmd" ) + done < <(grep -E "$query" "$HISTFILE" | sort | uniq) + if (( "${#cands[@]}" == 1 )); then + funcbody="${cands[0]}" + else + select funcbody in "${cands[@]}"; do break; done + fi + eval "$name() { $funcbody; }" +} + +funcsave() { # funcsave FUNC FILENAME + (( $# == 2 )) || { + echo "Wrong number of arguments (need 2)" >&2 + return 1 + } + cat < "$2" +#!/usr/bin/env bash +# saved with 'funcsave' + +$(type "$1" | sed 1d) + +if [[ "\${BASH_SOURCE[0]}" == "\$0" ]]; then + [[ "\$DEBUG" ]] && set -x + "$1" "\$@" +fi +EOF + chmod +x "$2" +} -- cgit 1.4.1-21-gabe81