Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: additionally saving history for each directory?



>>>>> On December 22, 2020 Greg Klanderman <gak@xxxxxxxxxxxxxx> wrote:

> Thanks Bart!
>>>>> On December 20, 2020 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:

>> There's a patch set in workers/45326 that never got much review.

> I don't think that was it, but interesting..

>> However, the very simple example is in the zsh documentation under
>> "zshaddhistory".

> Hmm maybe that's what I saw.. I had been browsing the zsh manual
> recently, though I thought there were also some example key bindings.

> So if I add a hook something like the following to
> zshaddhistory_functions:

> save_per_directory_history () {
>   setopt localoptions incappendhistory
>   fc -p -a .zsh_local_history 1000
>   print -sr -- ${1%%$'\n'}
> }

Hi all, realize this probably got overlooked during the holidays..
should it work to do it this way as well as the suggestion in the zsh
manual, i.e.

zshaddhistory() {
  print -sr -- ${1%%$?\n?}
  fc -p .zsh_local_history
}

which prints to the normal history, then switches to the local history
and relies on the normal save to save to the now default local
history.

My version is trying to write directly to the local history file from
the hook, then allow the normal history save to go to the normal
history file.  My version is creating the local history file, but
nothing gets written.  The normal history is updated.

Also if anyone has any key bindings for searching/traversing an
alternate local history file please post!

thanks,
Greg

> that should also work?  Seems a little cleaner to write to the per-
> directory history file from the hook, and rely on the normal history
> writing mechanism to write to the normal history.

> Should I 'return 0' to be sure that the normal history is updated, to
> guard against the off chance the print might return an error?

> Also in practice I'll write the local history to
> ~/.zsh-local-history/${PWD//\//##} or something like that to keep them
> all in one place and avoid littering the filesystem.

> So this will incur the cost of reading the per- directory history file
> on every command execution?  Or does zsh internally cache some number
> of recent history file sets?

> Hmm I see the local history files getting created, but they are all
> empty and remain so as I run various commands.  I added a print of $1
> in the hook and it does contain the command being run as expected.
> What am I doing wrong?

> thank you,
> Greg




Messages sorted by: Reverse Date, Date, Thread, Author