Issue
I'm trying to setup my history in zsh. I have activated option like HIST_IGNORE_ALL_DUPS which removes duplicated commands in the history.
But I am also looking for some option that can remove commands that don't exist which return 127 "command not found".
Solution
There's no such option in Zsh, but this can be easily achieved with the zsh-hist
plugin:
autoload -Uz add-zsh-hook
command-not-found () {
# -f: force
# -s: silent
# -1: most recent history item
(( ? == 127 )) &&
hist -fs delete -1
}
add-zsh-hook precmd command-not-found
This will automatically delete the last command line from history, if it returned 127
.
Alternatively, in addition to deleting it, you can also load the deleted command into the editing buffer, so you can immediately fix whatever typo you made, by using hist fix
instead of hist delete
:
autoload -Uz add-zsh-hook
command-not-found () {
(( ? == 127 )) &&
hist -fs fix -1
}
add-zsh-hook precmd command-not-found
Answered By - Marlon Richert Answer Checked By - Mary Flores (WPSolving Volunteer)