Skip to content

Instantly share code, notes, and snippets.

@kentquirk
Created August 9, 2018 02:27
Show Gist options
  • Save kentquirk/8061a76314488573da6b5a6468fe752f to your computer and use it in GitHub Desktop.
Save kentquirk/8061a76314488573da6b5a6468fe752f to your computer and use it in GitHub Desktop.
persistent history
#################################################################
# keep a permanent history of commands typed across all windows.
# This uses the history command to match the part that was typed,
# then uses BASH_REMATCH to extract that part and store it in the
# .persistent_history file. This is run automatically every time
# the prompt is redisplayed.
log_bash_persistent_history() {
[[
$(history 1) =~ ^[[:space:]]*[0-9]+[[:space:]]+(.*)$
]]
date_part=$(date "+%Y-%m-%dT%H:%M:%S")
command_part="${BASH_REMATCH[1]}"
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]; then
echo $date_part "|" "$command_part" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
fi
}
# Stuff to do on PROMPT_COMMAND
# persistent history and set the title of the window to the current directory
run_on_prompt_command() {
log_bash_persistent_history
echo -ne "\033]0;${PWD##*/}\007"
}
PROMPT_COMMAND="run_on_prompt_command"
# gets the last 10 matching unique commands from history, sorted by recency
phistory() {
grep $@ ~/.persistent_history | sort -k 2 |uniq -f 2 |sort |head -10
}
#######################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment