Deleting single entry from history in zsh — Terminal

terminalzsh

Is there a straight forward way of deleting a single entry from the shell history in the "new" zsh shell?

I know fc does not support this and I can't even figure out where Apple now stashes the history file (to cope with multiple terminal sessions).

Best Answer

You can either use:

open $HISTFILE && exit

which usually opens TextEdit (or another default text editor - depends on your environment) and remove the line (and the last temporary line containing something like : 1583100426:0;open $HISTFILE && exit) and save the file ("Save Anyway").

or - if you know some exclusive content of the line (in my example below PASSWORD):

setopt HIST_IGNORE_SPACE
 LC_ALL=C sed -i '' '/PASSWORD/d' $HISTFILE
  • the first command sets the option to not add commands prepended by spaces in the history file
  • the second command (prepended by a space!) removes all lines containing PASSWORD from the history file

Both commands won't remove unwanted lines in zsh history files in e.g. Time Machine backups!