In my related post many years ago, I found a solution how to comment out "dangerous" commands saved in bash history, so that I do not execute them accidentally.
What would be the best solution to implement the same in zsh
?
Does zsh
provide some functionality which I could use for this purpose?
I assume, zsh
beieng more flexible, this should be easier in zsh
.
For reference, this is what I have been using in bash
(based on accepted answer from Stéphane Chazelas):
fixhist() {
local cmd time histnum
cmd=$(HISTTIMEFORMAT='<%s>' history 1)
histnum=$((${cmd%%[<*]*}))
time=${cmd%%>*}
time=${time#*<}
cmd=${cmd#*>}
case $cmd in
(cp\ *|mv\ *|rm\ *|cat\ *\>*|pv\ *|dd\ *)
history -d "$histnum" # delete
history -a
[ -f "$HISTFILE" ] && printf '#%s\n' "$time" " $cmd" >> "$HISTFILE";;
(*)
history -a
esac
history -c
history -r
}
UPDATE:
While the accepted solution works, it has undesired side effects. In particular, the following history options specified in zshrc
are now being ignored
setopt histignorespace
setopt histreduceblanks
How can I make them work again ?
Best Answer
Sure, use the
zshaddhistory
hook function and disable regular history handling.Tested thusly on zsh 5.0.8
This appears to work with the
extendedhistory
option set, as well.