How to prevent a command in the zshell from being saved into history?
Use the HIST_IGNORE_SPACE option.
setopt HIST_IGNORE_SPACE
man zshoptions
HIST_IGNORE_SPACE
Remove command lines from the history list when the first character on the line is a space, or when one of the expanded aliases contains a leading space. Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line. If you want to make it vanish right away without entering another command, type a space and press return.
If you desire more granular control over what's added to ZSH history, you can define the zshaddhistory
function in .zshrc
. The following definition uses a regex to define a pattern to ignore:
function zshaddhistory() {
emulate -L zsh
if ! [[ "$1" =~ "(^ |^ykchalresp|--password)" ]] ; then
print -sr -- "${1%%$'\n'}"
fc -p
else
return 1
fi
}
Note that the behavior from man zshopts
under HIST_IGNORE_SPACE
is still present:
Note that the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line.
So to test it, you would have to hit an extra [Enter]
. This removes the command both from the output of history
, and also the ↑ arrow history.