Skip to content

Instantly share code, notes, and snippets.

@MartinHeinz
Created April 1, 2024 10:10
Show Gist options
  • Select an option

  • Save MartinHeinz/d233819a1041eae1700fa28ac8937229 to your computer and use it in GitHub Desktop.

Select an option

Save MartinHeinz/d233819a1041eae1700fa28ac8937229 to your computer and use it in GitHub Desktop.
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="ys"
HISTFILE="$HOME/.zsh_history"
HISTSIZE=10000000
SAVEHIST=10000000
HISTORY_IGNORE="(ls|cd|pwd|exit|cd)*"
HIST_STAMPS="yyyy-mm-dd"
bindkey "^E" history-incremental-search-backward
setopt EXTENDED_HISTORY # Write the history file in the ':start:elapsed;command' format.
setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits.
setopt SHARE_HISTORY # Share history between all sessions.
setopt HIST_IGNORE_DUPS # Do not record an event that was just recorded again.
setopt HIST_IGNORE_ALL_DUPS # Delete an old recorded event if a new event is a duplicate.
setopt HIST_IGNORE_SPACE # Do not record an event starting with a space.
setopt HIST_SAVE_NO_DUPS # Do not write a duplicate event to the history file.
setopt HIST_VERIFY # Do not execute immediately upon history expansion.
setopt APPEND_HISTORY # append to history file (Default)
setopt HIST_NO_STORE # Don't store history commands
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks from each command line being added to the history list.
plugins=(git fzf)
source $ZSH/oh-my-zsh.sh
export FZF_DEFAULT_COMMAND='ag --hidden -g ""'
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
@tinder-cfuller
Copy link

Great article! πŸ‘ https://martinheinz.dev/blog/110

Some notes you may consider including in the blog post:

  • INC_APPEND_HISTORY is not needed when SHARE_HISTORY is set since sharing history requires writing to the history file immediately

  • Typically you would only enable one of: HIST_IGNORE_DUPS/HIST_SAVE_NO_DUPS/HIST_IGNORE_ALL_DUPS since their behaviors are unique:

    • HIST_IGNORE_DUPS only ignores consecutive duplicates, so if you enter ls, then cd, then ls again, the second ls will be saved because it is not consecutive
    • HIST_SAVE_NO_DUPS prevents a command from being saved if it is a duplicate of any existing entry in the history, but it doesn't remove earlier instances of a command when a new one is entered
    • HIST_IGNORE_ALL_DUPS ensures that only the most recent instance of any command is preserved by removing earlier duplicates from the history whenever a command is re-entered
  • HIST_NO_STORE is just another name for HIST_IGNORE_SPACE so only one needs to be set (I prefer HIST_IGNORE_SPACE since it explicitly mentions the space which helps to remember what this option does)

@tinder-cfuller
Copy link

Just noticed there are two cd commands in HISTORY_IGNORE="(ls|cd|pwd|exit|cd)*"

Nonetheless, I kindly suggest testing to ensure that syntax is working as expected. I thought the syntax needed to be something like:

HISTORY_IGNORE=('ls' 'ls *' 'cd' 'cd *' 'pwd' 'exit')

@etorres07
Copy link

Thanks a lot for the article!

In case anyone runs into the same issue, I had to install fzf in order to have the ZSH fzf (just fzf for me, instead of git fzf) plugin work. Otherwise, I was prompted to specify FZF's installation dir.

Cheers

@italovieira
Copy link

Just noticed there are two cd commands in HISTORY_IGNORE="(ls|cd|pwd|exit|cd)*"

Nonetheless, I kindly suggest testing to ensure that syntax is working as expected. I thought the syntax needed to be something like:

HISTORY_IGNORE=('ls' 'ls *' 'cd' 'cd *' 'pwd' 'exit')

The syntax is indeed correct, zshparam(1) says:

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

@nastynaz
Copy link

nastynaz commented Aug 30, 2025

Great article! πŸ‘ https://martinheinz.dev/blog/110

Some notes you may consider including in the blog post:

  • INC_APPEND_HISTORY is not needed when SHARE_HISTORY is set since sharing history requires writing to the history file immediately

  • Typically you would only enable one of: HIST_IGNORE_DUPS/HIST_SAVE_NO_DUPS/HIST_IGNORE_ALL_DUPS since their behaviors are unique:

    • HIST_IGNORE_DUPS only ignores consecutive duplicates, so if you enter ls, then cd, then ls again, the second ls will be saved because it is not consecutive
    • HIST_SAVE_NO_DUPS prevents a command from being saved if it is a duplicate of any existing entry in the history, but it doesn't remove earlier instances of a command when a new one is entered
    • HIST_IGNORE_ALL_DUPS ensures that only the most recent instance of any command is preserved by removing earlier duplicates from the history whenever a command is re-entered
  • HIST_NO_STORE is just another name for HIST_IGNORE_SPACE so only one needs to be set (I prefer HIST_IGNORE_SPACE since it explicitly mentions the space which helps to remember what this option does)

This is not true. You can see in the source code that HIST_NO_STORE and HIST_IGNORE_SPACE do separate things. Search for histnostore and histignorespace to see the logic. I haven't checked the full logic for the HIST_IGNORE_xxx flags but if you search the source you can see that they do indeed do different things and may not trivially exclude each other.

Your statement about INC_APPEND_HISTORY and SHARE_HISTORY is also incorrect. See the block comment on line 1172 in the linked source that explains the logic when they are enabled together. The actual logic is that SHARE_HISTORY will only write immediately to the history file if it is not already locked:

    /*
     * For normal INCAPPENDHISTORY case and reasoning, see hbegin().
     */
    if (isset(SHAREHISTORY) ? histfileIsLocked() :
	(isset(INCAPPENDHISTORY) || (isset(INCAPPENDHISTORYTIME) &&
				     histsave_stack_pos != 0)))
	    savehistfile(hf, 0, HFILE_USE_OPTIONS | HFILE_FAST);
    unlockhistfile(hf); /* It's OK to call this even if we aren't locked */

@tinder-cfuller
Copy link

@nastynaz Thank you for correcting me πŸ‘ And I will definitely read through the information you shared when I have time to do so.

@tinder-cfuller
Copy link

tinder-cfuller commented Nov 3, 2025

Thanks again for the corrections. Very much appreciated.

I tried to find where on earth I mistakenly learned that HIST_NO_STORE was another name for HIST_IGNORE_SPACE, but could not find the source that I might have gotten that incorrect info from.

Regarding HIST_IGNORE_DUPS, HIST_SAVE_NO_DUPS and HIST_IGNORE_ALL_DUPS, my intention was not that they "exclude each other", but rather that in practical terms, only one of them is typically enabled.

And regarding INC_APPEND_HISTORY and SHARE_HISTORY, my comment was based on this man page content which instructs to turn off INC_APPEND_HISTORY when when using SHARE_HISTORY:

SHARE_HISTORY <K>

       This option both imports new commands from the history file, and also causes your typed commands to be
       appended to the history file (the latter is like specifying INC_APPEND_HISTORY, which should be turned
       off if this option is in effect).

https://github.com/zsh-users/zsh/blob/15f4567148943c5b733922d59b9c3eea26e26a42/Doc/Zsh/options.yo#L1084-L1089

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment