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

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

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").

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