6

I have added

export HISTIGNORE="ls:cd:pwd:exit:cd .."

to my .zshrc file.

Deleted .zsh_history and restarted terminal but it still wont ignore those commands.

enter image description here

tgreen
  • 1,720
  • 1
  • 16
  • 28

1 Answers1

12

The zsh shell doesn't use the HISTIGNORE environment variable. Instead, it has a HISTORY_IGNORE environment variable.

From the zshparam manual:

HISTORY_IGNORE

If set, is treated as a pattern at the time history files are written. Any potential history entry that matches the pattern is skipped. For example, if the value is fc * then commands that invoke the interactive history editor are never written to the history file.

Note that HISTORY_IGNORE defines a single pattern: to specify alternatives use the (first|second|...) syntax.

So in your case, you would want to do

HISTORY_IGNORE="(ls|cd|pwd|exit|cd ..)"

or something similar.

Notice that this affects only history written to the history file, not the history in the currently active shell session, as far as I can see.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • Doesn't seem to be working still. I am using .oh-my-zsh, don't know if that changes those variables. I was following this guys examples https://github.com/bradp/dotfiles/blob/master/sourced/exports which is why I was thinking it would work that way. But I am not sure anymore. – tgreen Jul 24 '16 at 07:02
  • Well, setting `SAVEHIST` to a positive number, making sure that `HISTFILE` is set to the name of a history file, and setting `HISTORY_IGNORE` to `"(cd|ls)"` definitely works as advertised by the `zsh` manual in any case. – Kusalananda Jul 24 '16 at 07:40
  • 1
    Thank you Kusalananda. I think you are right. I thought and was hoping that this would stop those commands from being shown when I ran 'history' or when i hit the up key. They are not in the history file though so i guess it is working. – tgreen Jul 24 '16 at 09:08
  • @tgreen Did you figure out how to make it stop showing ignored entries when pressing up/down? – lonix Jan 18 '20 at 04:54
  • The docs are not very clear. What means "pattern" is that a regex? – kev Mar 26 '20 at 21:49
  • 1
    @kev It would be a globbing pattern rather than a regular expression. – Kusalananda Mar 26 '20 at 21:50
  • `HISTORY_IGNORE` needs to be set to `"(cd|ls)*"` to match the whole command with arguments; `"(cd|ls)"` only applies to exact match like `cd` not `cd /foo/bar/`. – Saftever Apr 17 '21 at 05:32
  • 3
    @Saftever Yes, but if you set `HISTORY_IGNORE` to `"(cd|ls)*"` you would also weed out any _other_ command that starts with `cd` or `ls` though, so you may want `"(cd|cd *|ls|ls *)"`, or `"(cd(| *)|ls(| *))"`. – Kusalananda Apr 17 '21 at 06:31
  • @Kusalananda good point. For my use case, `"(cd|ls) *"` is sufficient because I mainly want to exclude the arguments from history, e.g. exclude `ls /top/secret/location`, but `ls` is ok. – Saftever Apr 17 '21 at 07:33