0

I'm writing a shell history application, and I'm experiencing issues with deleting a command from zsh history. When I was developing bash compatibility, I used readline to manipulate history - I first deleted command from readline's history, then wrote the history to a file. After the program exits, if the history was manipulated, then I do history -r and I get the expected behavior. Since zsh does not use readline and its history command significantly differs from the one that can be found on bash, I'm left with no other choice but to manipulate the history file directly. However, when I do that, the changes are not reflected until the shell is restarted. I have tried to use zsh's equivalent of history -r, which is supposed to be fc -R, but I did not get the expected results. While the command gets deleted from history after running that, pressing the up arrow (in order to go back to the previous command, which should be the one that invoked the program) brings me to a seemingly random command in the history.

I'd appreciate it if someone explained the odd behavior I'm experiencing. Any pointers in the right direction are also welcome.

adder
  • 3,512
  • 1
  • 16
  • 28

1 Answers1

0

I have solved this problem in my Zsh history editing plugin zsh-hist by not touching the histfile directly, but instead doing everything through Zsh's own mechanisms:

  1. Set $HISTORY_IGNORE locally to a pattern that matches the contents of the entries you want to delete.
  2. Use fc -W to write the history to file. Because of HISTORY_IGNORE, the unwanted entries will not be written.
  3. Use fc -p to create a new history from file.

Zsh documentation:

Marlon Richert
  • 5,250
  • 1
  • 18
  • 27
  • Unfortunately, I can't do this via zsh builtin mechanism as `fc` is a shell builtin. and I'm writing the application in Rust. You can have a look at it here: https://github.com/adder46/hstr-rs/issues/14 – adder Jan 08 '21 at 14:44
  • Aha, that wasn’t clear to me from your question. – Marlon Richert Jan 08 '21 at 18:40