How can I have zsh exclude certain commands from history?
Like, say, those that include the w开发者_C百科ord "production"?
function zshaddhistory() {
emulate -L zsh
if [[ $1 != *"production"* ]] ; then
print -sr -- "${1%%$'\n'}"
fc -p
else
return 1
fi
}
Put the above to a file that will be sourced when interactive shell starts (to .zshrc
or to a file that is sourced from .zshrc
like I do).
Alternate form (implicit addition to history):
function zshaddhistory() {
emulate -L zsh
if [[ $1 = *"production"* ]] ; then
return 1
fi
}
. Note:
print -sr -- "${1%%$'\n'}"
explicitly adds item to history. But the same thing implicitly does zsh if zshaddhistory
returns with zero exit code, so without fc -p
and with setopt nohistignoredups nohistignorealldups
(which is the default state) you will see unneeded duplicates in history.
emulate -L zsh
is here to make sure that emulation settings does not step in and change the interpretation of the function body. I put this line at the start of the every function I define in zsh configuration.
精彩评论