从 Zsh 历史记录中删除特定行

我想删除 Zsh 历史记录中的一个特定条目。

Zsh 的 fchistory没有任何删除条目的选项。我试过找 ~/.zhistory但是根本不存在。如何找到历史文件的位置并删除条目?

72553 次浏览

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

Clear zsh history on unix systems.

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

Clearing Zsh History (oh-my-zsh)


  • 关闭、退出并重新打开项目
  • 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. 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.

TL;DR

cat /dev/null > ~/.zsh_history

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.

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

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.

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

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.

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.

  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