Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an entry from the history in ZSH

Tags:

zsh

Let's say I ran a command using a zsh

echo "mysecret" > file

I can easily print the history including the entry numbers using the command fc -l:

1  echo "mysecret" >| file 

But how can I easily delete an entry from the history?

I cannot find a corresponding paragraph in man zshbuiltins.

like image 530
sandoz Avatar asked Aug 30 '11 13:08

sandoz


People also ask

How do you delete history on ZSH?

In my terminal, ZSH, there's a file called ~/. zsh_history , and a similar one for Bash. To remove the command, open that file and remove the entry from the list. Open a new terminal window, and the bad command is gone.

Where is ZSH command history stored?

ZSH is a popular shell built on top of bash. It stores your command history in the . zsh_history file in your home directory.

How do I delete bash history?

Clear all bash history by using history command: 'ls' command will display the list of the files and folders of the current location. 'clear' command will clear the terminal screen.


1 Answers

*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.

like image 126
rxw Avatar answered Sep 28 '22 05:09

rxw