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

Re: Feature Request: fc -C to clear history and reset counter

On 20.05.20 00:58, Bart Schaefer wrote:
> On Tue, May 19, 2020 at 1:22 PM Markus Näher <markus.naeher@xxxxxxx> wrote:
>> I have a second use case for clearing the history. I only started my
>> request with the use case that's easier to explain.
>> For bash, I wrote a function that allows me to edit the _whole_ history
>> (not only the last entry like fc), even reorder entries.
>> I just cannot work without that. All of my working style is adapted to
>> having that option.
> There's no reason you can't do that in zsh, but you're going to have
> some potential issues.  I've written about this in a zsh-users thread
> within the last couple of months.
> Copy-pasting from that thread ...
> The only safe way to directly edit the history is to make sure no
> other zsh is running that might rewrite it, and then set SAVEHIST=0 in
> your current shell before doing anything else.  (You can also do this
> by "fc -p" before invoking the editor, now that I think of it.)
> Once you are sure you have done that, then it should be OK to use an
> editor on the history file.  Be aware that multi-line events (such as
> "for" or "while" loops) are stored with lines terminated by backslash,
> so if you start deleting a line that ends in backslash you need to
> also delete all the adjacent lines that end in backslash, up to and
> including the next following line that does NOT end in a backslash.
> Single-line events never contain a trailing backslash.
> If you are using any of the setopts that store timestamped history
> entries, each event will be prefixed by a ":" command that ends at the
> next ";", with the timestamp between.  You should delete these along
> with the event you want to remove, and avoid altering any that are on
> other event lines.
> Something I forgot to mention in that other thread is that the zsh
> history file is stored in what's called "metafied" format.  Mikael
> Magnusson posted an "unmetafy.c" program back in 2015 which you should
> be able to find by a search of the zsh-users archives (subject: "Read
> file with escaped newlines into array").

Yes, I'm aware of the multi-write issues. That's why I'm setting
HISTFLE= in bash, which corresponds to SAVEHIST=0. I even plan to add
SAVEHIST=0 to my .zshrc. I never want my curated files to be overwritten.

I think I need to clarify that my history_edit bash function does not
edit the history file. By writing it to a temp file, clearing and
reading that file, I edit the _local_ history of the shell I'm currently
in, and other shells are unaffected, which is also important to me.
In fact, I mostly have at least 5 open shells. That's why I added the
.$$ (PID of the shell) to the temp filename, so I can even do
history_edit in all shells without interfering.

I only edit the history files when I do my curating. As my histories are
fairly mature, this does not occur very often.

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