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

Re: issues with saving history to file



On 2024-03-13 21:52:44 -0700, Bart Schaefer wrote:
> On Wed, Mar 13, 2024 at 8:05 AM Vincent Lefevre <vincent@xxxxxxxxxx> wrote:
> > IMHO, HISTFILE should just be unset by default; but if the user sets
> > this parameter, I suppose that the goal is to save the history.
> 
> The goal might be to read the history, not to write it.

To read the history? The zshparam(1) man page just says "to save":

  HISTFILE
      The file to save the history in when an interactive shell exits.
      If unset, the history is not saved.

And when I do

cventin:~> zsh -f
cventin% HISTFILE=~/.histfile

I just get the "HISTFILE=~/.histfile" in the history, while
~/.histfile already contains various commands.

> > Without INC_APPEND_HISTORY, the man page doesn't document when
> > precisely the history is saved. For instance, what happens if one
> > has a "print -s ..." in the .zlogout file?
> 
> It'd expect it to be lost, because the history is saved before
> .zlogout is read.   Try it and let us know.  Maybe it dumps core.

It is added to the history, but not saved. With

ls -l .histfile
sleep 1
print -s foo1
print -s foo2
print -s foo3
history
ls -l .histfile

in my .zlogout file, I get

-rw------- 1 vlefevre vlefevre 292 2024-03-15 13:17:11 .histfile
[...]
   27  foo1
   28  foo2
   29  foo3
-rw------- 1 vlefevre vlefevre 292 2024-03-15 13:17:11 .histfile

and indeed, .histfile does not contain these lines.

> > Moreover, with INC_APPEND_HISTORY, "print -s" does not put the entry
> > in the history file immediately, only after the next accept-line.
> 
> "print -s" doesn't write to the HISTFILE and isn't documented that
> way.  It explicitly says "Place the results in the history list".  The
> "history list" is the internal shell history, not the file.  The file
> is updated from the internal list either at shell exit or
> (incappendhistory) when a line is entered (i.e., conceptually upon
> accept-line, though in practice upon entering a complete command since
> updating at PS2 would leave an incomplete entry).

The documentation is not explicit.

> You can use the zshaddhistory hook to confirm this.

The documentation of the zshaddhistory hook is not clear. It says

  Executed when a history line has been read interactively,

But this is the case for any (possibly empty!) command line,
not just lines read from the history.

Then

  but before it is executed.

This is wrong. Empty command lines (which will not be executed) are
also handled by the zshaddhistory hook (contrary to the preexec hook).

And as a consequence, its example

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

has the effect to put empty command lines in the history, which
is not the usual behavior.

Moreover, the "fc -p .zsh_local_history" does not seem to work:
no .zsh_local_history file is created.

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)




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