Subject: Re: Memory usage of history?
From: Christian Neukirchen <[email-masked]>
To: [email-masked]
Date: Sat, 25 Jun 2016 19:43:48 +0200
List-Id: Zsh Workers List <zsh-workers.zsh.org>

Bart Schaefer <[email-masked]> writes:

> On Fri, Jun 24, 2016 at 6:47 AM, Dominik Vogt <[email-masked]> wrote:
>>
>> (A colleague
>> says his zshs use 200 MB memory each with a history size of a
>> million lines).
>
> To expand on Eric's answer, zsh reads the entire $HISTFILE and retains
> the last $HISTSIZE entries.  So a large $HISTFILE also slows down
> startup, even if it doesn't consume lots of memory.
>
> I can't imagine anyone having a million useful lines of history.  A
> few tens of thousands at most.

I use the following functions to create an additional, write-only,
daily history, and a helper to search it:

# Keep an archive of all commands typed.
# Initialize using:
#   cat /data/dump/juno/2015<->/home/chris/.zsh_history | sort -u | grep '^:' |
#     gawk -F: '{print $0 >> "chris@juno-" strftime("%Y-%m-%d", $2)}'
# 04sep2015  +chris+
mkdir -p ~/.zarchive
zshaddhistory() {
  local words=( ${(z)1} )
  local w1=$words[1]
  (( $+aliases[$w1] )) && w1=$aliases[$w1]
  if [[ -n $1 && $1 != $'\n' && $w1 != " "* ]]; then
    printf ': %s:0;%s' ${(%):-%D{%s}} "$1" >> \
      ~/.zarchive/${(%):-%n@%m-%D{%Y-%m-%d}}
  fi
}

# za WORDS... - search .zarchive for WORDS
za() {
  grep -a -r -e "${(j:.*:)@}" ~/.zarchive |
    sed 's/[ \t]*$//' |
    sort -r | sort -t';' -k2 -u | sort |
    sed $'s,^[^:]*/,,; s,::[^;]*;,\u00A0\u00A0,'
}
alias za=' za'

-- 
Christian Neukirchen  <[email-masked]>  http://chneukirchen.org