2

My history is 'polluted' with some specific lines that have typo's and such. For example: I often have to ssh to a certain server like so:

ssh [email protected]
> Connects succesfully

But I also make a typo sometimes:

ssh [email protected]
> Permission denied

Because I rely on history this typo is duplicated multiple times:

cat .zsh_history | grep [email protected]
> : 1510301683:0;ssh [email protected]
> : 1511166682:0;ssh [email protected]
> : 1511193552:0;ssh [email protected]
> : 1512730972:0;ssh [email protected]
> : 1516368993:0;ssh [email protected]
> : 1516802690:0;ssh [email protected]
> : 1519633368:0;ssh [email protected]

Is there an easy way to clean up my history purely by the results of the grep-command? (or perhaps there's some other, more clever solution?)

Update:

Please note: I am not looking for a way to delete line x from the history; I know there are plenty of articles out there that cover that. I'm simply looking for a (simple?) way to delete all lines that contain string y.

4
  • Possible duplicate of Remove a certain line from Bash history file
    – agc
    Apr 24, 2018 at 7:26
  • 1
    Not sure if it's a duplicate. I just want to clean my history by deleting all lines that contain string x. The linked question is how to delete by line numbers. Apr 24, 2018 at 8:33
  • You might be interested in zsh's option HIST_IGNORE_ALL_DUPS in order to not stor duplicates in your history at all: If a new command line being added to the history list duplicates an older one, the older command is removed from the list (even if it is not the previous event).
    – mpy
    Apr 24, 2018 at 16:36
  • @GielBerkers, Remove a certain line from Bash history file seems quite general, with answers using various methods. The simplest grep fix is in a comment by *mivk, which prints out the needed commands, (without actually running them): history | grep XYZ | grep -v grep | tac | awk '{print "history -d", $1}'
    – agc
    Apr 25, 2018 at 0:59

2 Answers 2

1

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

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

Alternatively, for a more comprehensive solution, try my Zsh Edit plugin.

1
  • 1
    @GielBerkers Does this answer your question/solve your problem? Jan 23, 2021 at 16:16
0

You can use this command to delete properly all lines with the string in the history :

sed -i '/string/d' .bash_history

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .