87

I'd like to remove a specific entry in my Zsh history.

Zsh's fc and history don't have any options to delete entries. I've tried looking for the ~/.zhistory but that doesn't exist. How can I go about finding the location of the history file and remove the entry?

2

13 Answers 13

84

You are looking in wrong File. Look at ~/.zsh_history not ~/.zhistory To view in which file your history is saved:

echo $HISTFILE

And delete:

rm $HISTFILE
6
  • 14
    Note that you have to log out and back in after rm $HISTFILE to see the changes. This doesn't answer the question though. It's not about deleting the whole history (history -c), it's about deleting a specific line (history -d [offset]). Mar 31, 2014 at 21:03
  • echo $HISTFILE goes to my bash history. ~/.zsh_history doesn't seem to have the entry I want to delete where else could it be?
    – Sten Kin
    Mar 31, 2014 at 21:06
  • Are you using zsh? If I echo $HISTFILE under both bash and zsh, I get different results. Mar 31, 2014 at 21:08
  • Yes I'm using zsh with oh-my-zsh, but looks like echo $HISTFILE points to my bash history file.
    – Sten Kin
    Mar 31, 2014 at 21:32
  • 1
    this is a serious bug... I should be able to delete a specific entry from the history imho.
    – Zabs
    Apr 29, 2016 at 12:54
55

Clearing Zsh History (oh-my-zsh)


  • close, quit and re-open iTerm
  • run nano .zsh_history
  • use the arrow keys to navigate to the part of your history you'd like to delete.
  • use the delete key to remove all unwanted history logs.
  • Once you've removed everything you'd like to remove, select control X to Exit.
  • You'll be prompted to Save the changes. If you're happy with your changes click shift Y.
  • You'll be asked where you'd like to save your changes. Select control T to save to File.
  • navigate to your .zsh_profile with your arrow keys and press enter.
  • Quit and restart iTerm.
  • type history to confirm the deletions.
  • You've successfully cleared your Zsh history.

1
  • 2
    Also vi with /thing to find. I don't know if that also works with nano.
    – doug
    Sep 4, 2020 at 6:44
24

Clear zsh history on unix systems.

 echo "" > ~/.zsh_history & exec $SHELL -l
7

You can use these commands to open the ZSH command's history(When you are in the home or ~ directory) and assume that you know how to use vim or nano :

nano ~/.zsh_history
vim ~/.zsh_history
open ~/.zsh_history

then you can delete the lines you want manually and save the file.

and if your zsh_history list is too long, for convenience use this:

  1. enable mouse move and line numbering in the Vim environment by adding this to .vimrc:
  2. open .vimrc:
 vim ~/.vimrc
  1. add these to .vimrc and save it(press ESC, enter ":" , write wq, and press enter):
:set number
set mouse=a
  1. use the mouse to scroll easily in zsh_history by using Vim.
  2. if you want to enable copy in Vim use holding shift on the keyboard.
0
6
  1. open ~/.zshrc
  2. add the following line

    alias clear_history='echo "" > ~/.zsh_history & exec $SHELL -l'
    
  3. Save and close the file

  4. Close the console or type zsh
    if you to see the result directly, but this will open another zsh shell in the old one
  5. Now you can clear the console typing clear_history

All the previous answers are good, this is simply the solution that worked for me.

1
6
  1. Type and run at the zsh command line, open ~/.zsh_history (This opens TextEdit on my Mac.)
  2. Delete any lines in the file
  3. Save and close the file
  4. Close/Exit the Zsh completely and restart the Zsh (this step is important!)
  5. Now, open zsh and the history command does not show the lines that you deleted
6

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

# Accepts one history line number as argument.
# Alternatively, you can do `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 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 -r "Deleted '$HISTORY_IGNORE' from history."
}

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

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

Alternatively, for a comprehensive, out-of-the-box solution, use my Zsh Hist plugin.

1
  • This is the correct answer to the OP question Nov 26, 2021 at 17:56
5

TL;DR

cat /dev/null > ~/.zsh_history

2

E.g. with vim you can easily delete the last n lines like this:

  1. Open file: vim ~/.zsh_history
  2. Go to the bottom of the file: G
  3. Mark lines: V -> move up with arrow key
  4. Delete: d
  5. Write & quit: :wq

Or you can just navigate with the cursor and delete any particular line with dd

2
  1. fc -W to write the history to the history file $HISTFILE.

  2. Edit $HISTFILE with an editor of your choice or use in-place sed to replace the lines you don't like with something else or blank lines.

  3. fc -R to read the history back from $HISTFILE.

Tips for the future:

  • Set setopt HIST_IGNORE_SPACE in your .zshrc file. With that option set, commands starting with a space won't end up in the history to begin with. Just prefix sensitive commands with a single space.

  • Set setopt INC_APPEND_HISTORY in your .zshrc file, then zsh will write new commands immediately to the history file. It's like calling fc -W before every new command but is more efficient (it won't write the entire history, just add a single line to the end of the file). That way the history file always reflects the current history and you can edit directly, then call fc -R. Note that you can then also call fc -IR at any time in other instances of zsh (e.g. another terminal) and immediately have the same history there available as well (nice way to easily sync history between instances).

  • Alternatively you can enable setopt SHARE_HISTORY, which has the same effect as INC_APPEND_HISTORY, except that it also calls fc -R after every command, so basically there is no in-memory history, the history is always written to and read from file and that across all instances, so all zsh instances share exactly the same history at all times and editing the history file also edits the history of all zsh instances immediately.

0

Open .zsh_history with your favourite editor and save keystrokes.

e.g. subl .zsh_history will open up history in Sublime editor and then delete whatever you want. You can use TextEdit or other editors also.

0

This worked for me: LC_ALL=C sed -i '' '/line/d' $HISTFILE

Replace "line" with what you want deleted.

From this answer: https://stackoverflow.com/posts/13661794/revisions

1
  • When you do not want the complete line to be deleted, you can also do: sed -i 's/secret/#####/g' ~/.zsh_history, which replaces the word secret by ###### (also in this last line you type to remove the secret from your history file!)
    – Luuk
    Oct 29, 2022 at 14:19
0

For ZSH

To locate the history file do :

echo $HISTFILE
  • Then simply edit the file and remove any lines you wish to be gone as you would with history -d id.
  • Save the file.
  • Open a new terminal and you should see that there is nothing to see anymore !

However I am amazed that history -d does not exists. If it does exists it's well hidden.

2
  • 1
    In zsh, history is just an alias for fc -l and fc -l -d does not delete anything, -d means print date timstamps. history -d is bashism.
    – Mecki
    Jun 30, 2023 at 9:36
  • @Mecki My man fc(1) doesn't even show -d option. I wonder if a zsh plugin exists to fix this.
    – Doctor
    Jul 19, 2023 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.