improvement
authorIan Kelling <ian@iankelling.org>
Tue, 24 Feb 2026 04:48:56 +0000 (23:48 -0500)
committerIan Kelling <ian@iankelling.org>
Tue, 24 Feb 2026 04:48:56 +0000 (23:48 -0500)
brc

diff --git a/brc b/brc
index 1793d64fd8fc1cd567b5b6682b2cd74c20ed455d..8a7daebe584dcb99c8611e7b989caf3bd0c5eb99 100644 (file)
--- a/brc
+++ b/brc
@@ -1582,7 +1582,7 @@ etailbg() {
   ta /var/log/exim4/mainlog /var/log/exim4/*main /var/log/exim4/paniclog /var/log/exim4/*panic -n 200 "$@"
   ngreset
 }
-
+# shellcheck disable=SC2120
 etail() {
   tail -F /var/log/exim4/mainlog /var/log/exim4/*main /var/log/exim4/paniclog /var/log/exim4/*panic -n 200 "$@"
 }
@@ -2340,10 +2340,38 @@ lower() { # make first letter of filenames lowercase.
   done
 }
 
-
-k() { # history search
-  grep -iP --binary-files=text "$@" ${HISTFILE:-~/.bash_history}  | tail -n 80 || [[ $? == 1 ]];
+# history search
+k() {
+  local grep_out1 grep_out2 histf1 histf2
+  local -a lines1 lines2
+  histf1=${HISTFILE:-~/.bash_history}
+  grep_out1=$(grep -iP --binary-files=text "$@" $histf1  | tail -n 80) || [[ $? == 1 ]]
+  if [[ $EUID == 0 ]]; then
+    histf2="/home/iank/${histf1##*/}"
+    grep_out2=$(grep -iP --binary-files=text "$@" $histf2  | tail -n 80) || [[ $? == 1 ]]
+  elif sudo -nv 2>/dev/null; then
+    histf2="/root/${histf1##*/}"
+    grep_out2=$(sudo grep -iP --binary-files=text "$@" $histf2  | tail -n 80) || [[ $? == 1 ]]
+  fi
+  if [[ $grep_out1 && $grep_out2 ]]; then
+    mapfile -t lines1 <<<"$grep_out1"
+    mapfile -t lines2 <<<"$grep_out2"
+    # in this case, just insert a chunk of secondary results in the middle.
+    if (( ${#lines1[@]} > 20 )); then
+      printf "%s\n" "${lines1[@]:20}"; printf "############ ^ %s #############\n"  $histf1
+      printf "%s\n" "${lines2[@]::20}"; printf "############ ^ %s #############\n"  $histf2
+      printf "%s\n" "${lines1[@]::20}"
+    else
+      printf "%s\n" "${lines2[@]}"; printf "########## ^ %s #########\n" $histf2;
+      printf "%s\n" "${lines1[@]}"
+    fi
+  elif [[ $grep_out2 ]]; then
+    printf "%s\n" "${lines2[@]}"; printf "### ^ %s\n" $histf2
+  elif [[ $grep_out1 ]]; then
+    printf "%s\n" "${lines1[@]}"
+  fi
 }
+
 ks() { # history search with context
   # args are an extended regex used by sed
   history | sed -nr "h;s/^\s*(\S+\s+){4}//;/$*/{g;p}" | tail -n 80 || [[ $? == 1 ]];