Hacker News new | ask | show | jobs
by danShumway 2296 days ago
Not GP, and my setup is a great deal less sophisticated than GP's.

However, in my `.bashrc`, I have:

  log_bash_persistent_history()
  {
      [[
          $(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
      ]]
      local date_part="${BASH_REMATCH[1]}"
      local command_part="${BASH_REMATCH[2]}"
      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
  run_on_prompt_command()
  {
      log_bash_persistent_history
  }

  PROMPT_COMMAND="run_on_prompt_command"
  HISTTIMEFORMAT="%d/%m/%y %T "
  alias phgrep='cat ~/.persistent_history|grep --color'
  alias hgrep='history|grep --color'
This doesn't save all of the information like the current shell, working directory, etc... but you could easily modify it to include that information. It also doesn't do all of the context-aware piping, you have to rely on grep. But just having a really good persistent history is itself insanely useful.

My setup was itself stolen from someone else on HN a few years ago, and I've since forgotten their name.

1 comments

If you want to have just persistent histry (really simple), you can use vanilla bash history something like. export HISTCONTROL=ignorespace shopt -s histappend export HISTSIZE=99999999 # Big enough number to never rotate history (or when you really don't care) export HISTFILESIZE=99999999 shopt -s checkwinsize export HISTTIMEFORMAT='%F %T '