Skip to content

Instantly share code, notes, and snippets.

@actuallymentor
Created August 24, 2023 13:24
Show Gist options
  • Save actuallymentor/13aace5d03eba993147cdd8a4f87ac2a to your computer and use it in GitHub Desktop.
Save actuallymentor/13aace5d03eba993147cdd8a4f87ac2a to your computer and use it in GitHub Desktop.
A history searching function for linux and mac
# History search helper
# usage: his query1 query2 queryn...
# example: his ssh 192 (search all ssh commands done to ips inclusing 192)
# example: his sed jsx react (search all sed commands that include "jsx" and "react")
function his() {
# Store the full history in a variable
# Command order: history, remove line numbers, remove leading and trailing whitespace, sort, remove duplicates
# Note that we are using a naive way of removing the line numbers
commandlog=$(history | grep -oE "[a-zA-Z]{1}.*" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sort | uniq)
# For every parameter provided to this function, run a case insensitive grep
for var in "$@";do
commandlog=$(echo "$commandlog" | grep -i "$var")
done
# Log out the results
echo "$commandlog"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment