In Bash I know putting a space before a command prevents it from being kept in the history, what is the equivalent for the zshell?
2 Answers
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.
-
10This wasn't working for me, until I read the whole text and realized it's awesomer than bash!!– 0fntAug 19, 2014 at 9:51
-
5And then, for the commands I wanted to prevent, I used aliases and prefixed a space: alias jrnl=" jrnl" Sep 23, 2014 at 21:26
-
2@Yar kill -9 that shell (if it doesn't do line-per-line history file writes) Oct 26, 2015 at 10:47
-
1@MichaelShigorin Thanks!
kill -9 $$
is indeed fantastic to avoid all the commands issued in current terminal session to be stored in the history. Mar 14, 2016 at 17:11 -
2@CMCDragonkai You could
unset HISTFILE
prior to your block of commands. You'd need to resetHISTFILE
afterward or open a new shell to keep commands in your history that you want. Jun 29, 2016 at 18:36
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.
See this plugin for keeping log entries, but mask sensitive data based on a regex: https://github.com/jgogstad/passwordless-history