Delete specific line from Zsh history

MacosZsh

Macos Problem Overview


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?

Macos Solutions


Solution 1 - Macos

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

echo $HISTFILE

And

rm $HISTFILE

Solution 2 - Macos

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.

[tag:osx][tag:terminal][tag:zsh][tag:oh-my-zsh]

Solution 3 - Macos

Clear zsh history on unix systems.

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

Solution 4 - Macos

  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.

Solution 5 - Macos

TL;DR

cat /dev/null > ~/.zsh_history

Solution 6 - Macos

You can use these commands to open ZSH command's history(When you are in 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.

Solution 7 - Macos

  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

Solution 8 - Macos

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

Otherwise, 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 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 additionally want to prevent all dc commands from being written to history, add the following in your ~/.zshrc file:

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

Solution 9 - Macos

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

Solution 10 - Macos

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.

Solution 11 - Macos

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

Solution 12 - Macos

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSten KinView Question on Stackoverflow
Solution 1 - MacosAmrit DhunganaView Answer on Stackoverflow
Solution 2 - MacosWebEpicView Answer on Stackoverflow
Solution 3 - MacosDonald DerekView Answer on Stackoverflow
Solution 4 - MacosGian0508View Answer on Stackoverflow
Solution 5 - MacosokandasView Answer on Stackoverflow
Solution 6 - MacosFarshadView Answer on Stackoverflow
Solution 7 - MacosFullStackCoderView Answer on Stackoverflow
Solution 8 - MacosMarlon RichertView Answer on Stackoverflow
Solution 9 - MacosTobView Answer on Stackoverflow
Solution 10 - MacosRajkiranView Answer on Stackoverflow
Solution 11 - MacosaakochView Answer on Stackoverflow
Solution 12 - MacosDoctorView Answer on Stackoverflow