I think you are trying to solve the wrong problem, or you are trying to solve a problem the wrong way. I could be wrong but to me if you need to delete your history to avoid an issue, there is probably something you could do differently.
However, I was curious to see if it is possible to come up with a solution. So I gave it a try and here is what I have:
let g:commands_to_delete_from_history = ['echo']
let lastHistoryEntry = histget('cmd', -1)
let lastCommand = split(lastHistoryEntry, '\s\+')
if (index(g:commands_to_delete_from_history, lastCommand) >= 0)
call histdel('cmd', -1)
autocmd CmdlineLeave * call DeleteCommandsFromHistory()
This code should be added to your vimrc (or even better you should put
DeleteCommandsFromHistory() in a dedicated autoload plugin, but that's not the point of this answer).
You will need to modify the list
g:commands_to_delete_from_history to include the commands you don't want to have in your history.
Then what the code does is the following:
It create an autocommand on the event
CmdLineLeave which is triggered each time to were in the command line mode (started with
:) and you leave it one way or another.
The autocommand calls the function
DeleteCommandsFromHistory(). This function gets the last entry in the history, split it to keep only the first word (e.g.
echo if the command was
:echo "foo"), and if the word is found in the
g:commands_to_delete_from_history list then it calls
histdel to delete the last entry from the history.
I tested the code very quickly and it seems to be working but there is probably some edge cases that I forgot but this is a proof of concept that you should tweak as you like.
Related help topics: