2

I set the HISTORY_IGNORE variable in ~/.zshrc and it correctly prevents those commands from being written to ~/.zsh_history.

But what I really want is for those entries not to show when pressing up and down. (Same as HISTIGNORE when using bash).

How do I accomplish that?

1 Answer 1

1

You need to write a hook for that. As it says in the HISTORY_IGNORE documentation:

Compare the HIST_NO_STORE option or the zshaddhistory hook, either of which would prevent such commands from being added to the interactive history at all. If you wish to use HISTORY_IGNORE to stop history being added in the first place, you can define the following hook:

zshaddhistory() {
  emulate -L zsh
  ## uncomment if HISTORY_IGNORE
  ## should use EXTENDED_GLOB syntax
  # setopt extendedglob
  [[ $1 != ${~HISTORY_IGNORE} ]]
}

Important findings from my own testing:

  • $1 includes a line break at the end. Be aware of this when writing your pattern for HISTORY_IGNORE. If it doesn't account for it, it won't match.
  • This function is applied whenever Zsh writes your history from its memory to your history file. Thus, it will retroactively remove lines from your history. Be careful with this!
6
  • I added that to /.zshrc but the commands I excluded in HISTORY_IGNORE still show up in <kbd>up</kbd> and <kbd>down</kbd>.
    – lonix
    Jun 24, 2020 at 10:07
  • As it says in the documentation for zshaddhistory: "If any of the hook functions returns status 1 (or any non-zero value other than 2, though this is not guaranteed for future versions of the shell) the history line will not be saved, although it lingers in the history until the next line is executed, allowing you to reuse or edit it immediately." Thus, after executing the command that you want history to ignore, first execute a new command and then try to see if the command you wanted to ignore is gone. Jun 24, 2020 at 10:19
  • Thanks, though that doesn't work for me either. Maybe a version or dependency problem. Does this actually work for you?
    – lonix
    Jun 25, 2020 at 10:48
  • Yes, it does. If use that function and set HISTORY_IGNORE='*', then nothing gets added to my history. I'm using Zsh 5.8. Jun 26, 2020 at 11:28
  • I just tested it and $1 includes a line break at the end for me. It might be that's why your pattern doesn't match. Jun 26, 2020 at 11:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .