Delete specific line from Zsh history
MacosZshMacos 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
toExit
. - You'll be prompted to
Save
the changes. If you're happy with your changes clickshift Y
. - You'll be asked where you'd like to save your changes. Select
control T
to saveto File
. - navigate to your
.zsh_profile
with yourarrow 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
-
open
~/.zshrc
-
add the following line
alias clear_history='echo "" > ~/.zsh_history & exec $SHELL -l'
-
Save and close the file
-
Close the console or type
zsh
if you to see the result directly, but this will open another zsh shell in the old one -
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
- read this for more info https://www.techrepublic.com/article/how-to-effectively-clear-your-bash-history/
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
- Type and run at the zsh command line, openĀ ~/.zsh_history (This opens TextEdit on my Mac.)
- Delete any lines in the file
- Save and close the file
- Close/Exit the Zsh completely and restart the Zsh (this step is important!)
- 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:
- Open file:
vim ~/.zsh_history
- Go to the bottom of the file:
G
- Mark lines:
V
-> move up with arrow key - Delete:
d
- 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.