Skip to content

Instantly share code, notes, and snippets.

@PaulFurtado
Created September 24, 2015 02:01
Show Gist options
  • Save PaulFurtado/7e7e762039756a732639 to your computer and use it in GitHub Desktop.
Save PaulFurtado/7e7e762039756a732639 to your computer and use it in GitHub Desktop.
bashrc custom history
__lastcmd="$(history 1 | sed 's/^[ ]*[0-9]\+[ ]*//')"
HISTORY_FILE=~/.bash_eternal_history
function __prompt_cmd() {
local lastexit="$?"
local cmdstr="$(history 1 | sed 's/^[ ]*[0-9]\+[ ]*//')"
local logstr="DATE='$(date -Iseconds)' PID=$$ USER=$USER PWD='$PWD' EXIT_CODE=$lastexit COMMAND=$cmdstr"
if [[ "$cmdstr" != "$__lastcmd" ]] ; then
__lastcmd="$cmdstr"
echo "$logstr" >> $HISTORY_FILE
if [[ -n "$lastexit" && "$lastexit" -ne 0 ]]; then
local outtext="*** WARNING: The exit code was: $lastexit ***"
local RCol='\e[0m'
local Red='\e[0;31m'
echo -e "$Red$outtext$RCol"
fi
fi
}
PROMPT_COMMAND=__prompt_cmd
function histgrep {
(grep "$@" $HISTORY_FILE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment