3

I am trying to ignore some sensitive commands from being saved in history file in zsh.

HISTSIZE=1000                 # 1000 lines of history within the shell
SAVEHIST=1000                 # 1000 lines of history in $HISTFILE
HISTFILE=${HOME}/.zsh_history # Save history to ~/.zsh_history
## Ignore save in $HISTFILE, but still in the shell
HISTORY_IGNORE='([bf]g *|cd ..|l[a,l,s,h,] *|less *|vi[m,] *)'

But it still shows in history file:

% rm .zsh_history 
% ls              
Desktop  Documents  Downloads  Music  peda  Pictures  Public  Templates  Videos
% l
Desktop/  Documents/  Downloads/  Music/  peda/  Pictures/  Public/  Templates/  Videos/
% cat .zsh_history
ls
l
cat .zsh_history

I have read the zshparam page but it doesn't help.

Is the part l[a,l,s,h,] * right?

mja
  • 998

1 Answers1

4

l[a,l,s,h,] * does not match l because [] is not a comma-separated list of things to expand to - that's the syntax for brace expansion. With [...], [a,l,s,h,] is the same as [alsh,] and does not mean any of alsh or nothing. You need l[alsh]#.

For ls, the space seems to be breaking this match, even though everything after that is optional. So in practice, this seems to ignore ls only if it has an argument:

~ ls foo
ls: cannot access 'foo': No such file or directory
~ tail ~/.histfile
HISTORY_IGNORE='([bf]g *|cd ..|l[a,l,s,h,]*|less *|vi[m,] *)'
l
tail ~/.histfile
l
tail ~/.histfile

It seems you need to make the * optional as well:

HISTORY_IGNORE='([bf]g *|cd ..|l[alsh]#( *)#|less *|vim# *)'
muru
  • 197,895
  • 55
  • 485
  • 740
  • Do you know where to find the full documentation about HISTORY_IGNORE? – mja Jan 28 '18 at 03:15
  • 1
    @MarkJ.Adams zshparam is about it. That said, the manpage mentions [[ $1 != ${~HISTORY_IGNORE} ]] in the function on removing entries from history, and I think that test matches up pretty well with the actual behaviour of HISTORY_IGNORE on command lines – muru Jan 28 '18 at 03:18