How to remove an entry from the history in ZSH
Asked Answered
zsh
S

6

112

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.

Superimposed answered 30/8, 2011 at 13:31 Comment(2)
What is this doing on StackOverflow as opposed to SuperUser?Jab
Duplicate of #22772458Duane
S
134

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

Subsolar answered 1/12, 2012 at 17:27 Comment(3)
NB on osx/BSD you need to put empty quotes after the -i to explicitly specify no backup file, thus: sed -i '' '/S3_SECRET/d' $HISTFILE. This won't work on linux. #5694728Entropy
And if you get 'illegal byte sequence' then set LC_ALL=C, thus: LC_ALL=C sed -i '' '/S3_SECRET/d' $HISTFILE #11288064Entropy
Don't forget to reload zsh, with source .zshrc.Behl
D
86

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.

Dannadannel answered 1/9, 2011 at 1:53 Comment(3)
Worked for me. I did have to restart my terminal for it picked up on the changes.Mcardle
For bonus points, avoid loading the history file by setting the environment variable HISTFILE=/dev/null, or just use another shell. If zsh is the login shell, login and exec sh, then edit ~/.zhistory.Parthenia
It does work! remove command from ~/.zsh_history, then restart zsh.Inarch
H
46

If you use the HIST_IGNORE_SPACE option in zsh you can prepend commands with a space " " and they will not be remembered in the history file. If you have secret commands you commonly use you can do something along the lines of: alias hiddencommand=' hiddencommand'.

Handwoven answered 19/9, 2011 at 23:45 Comment(2)
put setopt histignorespace in your ~/.zshrcMouser
Thanks for histignorespace. Very useful !Edgardo
E
15

If you only want to make an occasional deletion, I think that it's easier to manually edit your .zsh_history.

In a zsh terminal:

  1. Close the terminal session with the command to delete.
  2. open a new session,
  3. open ~/.zsh_history with a text editor (pico, Emacs, vim...),
  4. delete the faulty lines,
  5. close the editor, close the terminal session and open a new one,
  6. enter history and the unwanted history item will be gone.

(Make sure the editor hasn't backed up the previous .zsh_history instance.)

(Solution based on https://til.hashrocket.com/posts/zn87awopb4-delete-a-command-from-zsh-history-)

Eichman answered 13/8, 2021 at 16:29 Comment(0)
D
9

This function will remove any one line you want from your Zsh history, no questions asked:

# Accepts one history line number as argument.
# Use `dc -1` to remove the last line.
dc () {
  # Prevent the specified history line from being 
  # saved.
  local HISTORY_IGNORE="${(b)$(fc -ln $1 $1)}"

  # Write out the history to file, excluding lines that
  # match `$HISTORY_IGNORE`.
  fc -W

  # Dispose of the current history and read the new 
  # history from file.
  fc -p $HISTFILE $HISTSIZE $SAVEHIST

  # TA-DA!
  print "Deleted '$HISTORY_IGNORE' from history."
}

If you want to additionally prevent all dc commands from being written to history, add the following in your ~/.zshrc file:

zshaddhistory() {
 [[ $1 != 'dc '* ]]
}

Update

I've now published a more comprehensive solution as a plugin: https://github.com/marlonrichert/zsh-hist

Duane answered 19/8, 2020 at 20:47 Comment(0)
M
7

tldr:

vi $HISTFILE

more details:

run vi $HISTFILE

SHIFT + g — to go to the end

dd — to remove line

:wq — to save and exit

reload session or open a new tab to see changes

Monochasium answered 2/2, 2023 at 4:40 Comment(1)
how to reload a ZSH session?Chemotherapy

© 2022 - 2024 — McMap. All rights reserved.