Conveniently remove lines from zsh history

What is the problem

Sometimes you type something on the console and afterwards realise that you do not want that bit to be stored in you (zsh) history file. For one, I sometimes start typing my passwords a bit early :-)

The fix

As I require this functionality more often I have written a short, but convenient zsh function to perform this bit of magic for me. Add the function as follows:

$ nano ~/.zshrc

Or in other words: open the file ~/.zshrc i nyour favourite text editor. Then add the following to this file (I recommend to add it near the bottom):

# To remove any command from the zsh history file
histrm() { LC_ALL=C sed --in-place '/$1/d' $HISTFILE }

After reloading the zsh environment (log-out and -in, or open a new console window) you can remove the offending line using:

$ histrm <text>

in which the text is a part of the command that should be removed.

Remaining issue

I still need to add a way to not add the ‘histrm ' command to the history as this already might contain too much information.

~ Patrick