Delete specific line from Zsh history
Asked Answered
C

13

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?

Coessential answered 31/3, 2014 at 20:52 Comment(2)
Duplicate of #7244483 . Oh-My-Zsh is just an add-on for Zsh. It doesn't change anything about how Zsh's history works.Protactinium
omz does replace history with omz_history via an alias.Watch
M
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
Monnet answered 31/3, 2014 at 21:1 Comment(6)
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]).Capitulation
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?Coessential
Are you using zsh? If I echo $HISTFILE under both bash and zsh, I get different results.Capitulation
Yes I'm using zsh with oh-my-zsh, but looks like echo $HISTFILE points to my bash history file.Coessential
This did not work for me at all - even with the file removed, history still shows the same output. It does not appear to have any correlation to $HISTFILE at all.Hemichordate
this is a serious bug... I should be able to delete a specific entry from the history imho.Oswin
P
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.

Pediculosis answered 6/1, 2016 at 5:51 Comment(1)
Also vi with /thing to find. I don't know if that also works with nano.Ricker
S
24

Clear zsh history on unix systems.

 echo "" > ~/.zsh_history & exec $SHELL -l
Stoplight answered 5/9, 2015 at 21:4 Comment(0)
E
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.
Easement answered 7/2, 2020 at 9:38 Comment(0)
P
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.

Pheni answered 1/6, 2017 at 13:8 Comment(1)
Really handly and helpful answer.Piffle
P
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.

Protactinium answered 19/8, 2020 at 20:44 Comment(1)
This is the correct answer to the OP questionRevis
H
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
Higbee answered 10/3, 2021 at 5:51 Comment(0)
H
5

TL;DR

cat /dev/null > ~/.zsh_history

Hazy answered 1/1, 2018 at 22:39 Comment(0)
A
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

Allan answered 18/8, 2020 at 15:28 Comment(0)
C
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.

Cudbear answered 30/6, 2023 at 10:9 Comment(0)
R
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.

Renovate answered 3/12, 2019 at 12:47 Comment(0)
C
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

Contemptible answered 6/1, 2020 at 18:30 Comment(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!)Cruzcruzado
L
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.

Lipase answered 20/1, 2021 at 12:12 Comment(2)
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.Cudbear
@Cudbear My man fc(1) doesn't even show -d option. I wonder if a zsh plugin exists to fix this.Lipase

© 2022 - 2024 — McMap. All rights reserved.