Skip to content

Instantly share code, notes, and snippets.

@locriani
Created February 14, 2019 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save locriani/724e26fc09379d0c2c78f807797a4177 to your computer and use it in GitHub Desktop.
Save locriani/724e26fc09379d0c2c78f807797a4177 to your computer and use it in GitHub Desktop.
Bash History Scripts
source ~/.bash/history.sh
# frozen_string_literal: true
HISTORY_REGEX = /^(.*\.history\/\d{4}\/\d{2}\/\d{2}\/\d+).#(\d+)$\n^.*\.*\.history\/\d{4}\/\d{2}\/\d{2}\/\d+:(.*)$/
PID_REGEX = /^.*\.history\/\d{4}\/\d{2}\/\d{2}\/(\d+).*$/
results = ARGF.read.scan(HISTORY_REGEX)
results.map! do |result|
date = Time.at(result[1].to_i)
file = result[0]
pid = file.scan(PID_REGEX)[0][0]
command = result[2]
[date, "#{pid} #{date.strftime('[%F %T]')} #{command}"]
end
results.sort.each {|r| puts r[1] }
# setting HISTSIZE and HISTFILESIZE to empty allows no limit.
export HISTSIZE=
export HISTFILESIZE=
export HISTTIMEFORMAT="[%F %T] "
# ignore:
# 1 character and 2 character commands ( ?:?? )
# ls, bg, fg, history, exit, pwd, clear commands
# any command preceded with a space (` cat file` would be ignored) ([ \t]*)
export HISTIGNORE="?:??:ls:[bf]g:history:exit:pwd:clear:[ \t]*"
# /Users/locriani/.history/2018/05/02/62135
export HISTFILE="${HOME}/.history/$(date -u +%Y/%m/%d/$$)"
mkdir -p "${HISTFILE%/*}"
# Mac OS X does some dumb things, so you need to make sure the prompt uses -w, and not -a
export PROMPT_COMMAND="history -w; $PROMPT_COMMAND"
# Mac OS X does some dumb things, so you need to make sure this file exists, or
# any history setting changes you make will be ignored
touch "${HOME}/.bash_sessions_disable"
# [2018-05-02 00:33:04] xargs
h() {
grep -r -H -B1 $@ ${HOME}/.history | \
ruby ${HOME}/.bash/history.rb | \
grep -B1 --color -E "$@|$$|\$"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment