10
6
Problem
I'm using oh-my-zsh and Antigen to manage my Zsh.
Sometimes I will accidentally enter a wrong/failed command into Zsh and it is very annoying when the zsh-autosuggestions plugin throws up these commands.
For e.g., say I typed
gut status
rather than git status
.
Now, as I attempt to type the correct command again, once I typed g
, the autosuggestion plugin will throw up gut status
as that is the closest match in recent history. But that is a failed command!
What I want to do is to keep these failed commands from "polluting" my history so the autosuggestion plugin will not suggest them.
What I have done
I've searched for this and the closest match to keeping out entries from the history are that of setting HIST_IGNORE_SPACE, or methods to ignore specific commands, or to delete commands manually after entering them. See this and this. But as @Adaephon 52 noted, this takes place after the deed is done.
What I'm considering doing
What I have in mind right now is to write a function that gets triggered every time a failed command is entered into Zsh to delete the latest entry from history. However, I'm not familiar enough with Zsh to know whether this sort of trigger exists or how to go about doing it.
Note on the accepted solution
For those who are interested in this problem, the accepted solution works in the sense that failed commands are not stored in the history file. fc -l
or history
confirms this.
However, it seems that the zsh-autosuggestions plugin is doing its own local caching of sorts (or is generating suggestions using other mechanism), so it will still suggest the failed commands, but only if the session is not over. When the user starts a new instance of the shell, when presumably the plugin has to load the cached history, it no longer suggests the failed commands (unless you enter them again, of course).
The proposed solution is technically correct as I (mistakenly) framed my question in such a way that it only requires an answer that stops Zsh from storing the failed commands and the accepted solution does that (albeit in a limited way; See link in solution for details).
Anyone who is familiar with shell scripting (I'm not) and motivated enough can check the zsh-autosuggestions script.
This does stops the caching of failed commands. But it seems that the zsh-autosuggestions plugin is doing its own local caching of recent entries so it still throws up these suggestions as long as the session is not over. Once I open a new shell, when presumably it has to load the cached history, it no longer suggests them. This solved part of the problem and I guess I have to check with the zsh-autosuggestions plugin's maintainer to resolve the remaining issue. Still, thanks! – lightalchemist – 2015-04-17T02:21:26.530
where do you put this function? – Daniel – 2019-08-20T05:44:53.990
1@Daniel: In any config file zsh will read upon startup, typically your
~/.zshrc
. – mpy – 2019-08-20T17:10:42.757