How to remove an entry from the history in ZSH

*BSD/Darwin (macOS):

LC_ALL=C sed -i '' '/porn/d' $HISTFILE

Linux (GNU sed):

LC_ALL=C sed -i '/porn/d' $HISTFILE

This will remove all lines matching "porn" from your $HISTFILE.

With setopt HIST_IGNORE_SPACE, you can prepend the above command with a space character to prevent it from being written to $HISTFILE.

As Tim pointed out in his comment below, the prefix LC_ALL=C prevents 'illegal byte sequence' failure.


I don't know if there is some elegant method for doing this, but in similar situations I have logged out (allowing zsh to empty its buffer and write my history to file), then logged in, and finally manually edited ~/.zsh_history, deleting the "dangerous" line.