Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to (properly) remove the last entry from history with command_not_found_handler

On Jul 20, 11:04am, Jochen Keil wrote:
} function command_not_found_handler()
} {
}     sed -i '$d' $HISTFILE
}     return 127
} }
} However, I'm not sure if it's ok to manipulate the history file directly
} with sed, or if I'm risking corruption.

This is likely to be a problem if the failing command spans more than
one line (e.g., a "for" loop with command-not-found in the middle of
it) or if you are using SHARE_HISTORY where there's a race condition
among shells reading the history and shells (re)writing it.

There really isn't any way to "properly" remove an entry once it has been
written to the history file.  You might try some variation of this:

  zshaddhistory() {  whence ${${(z)1}[1]} >/dev/null || return 2 }

There are still problems with that for multi-line commands but the right
way to achieve your desired behavior is to avoid writing out the commands
you don't want, not to write them and then delete them again.

Messages sorted by: Reverse Date, Date, Thread, Author