7

On macOS, there is a sessions directory that combines commands from multiple zsh sessions into a single history file.

Is it necessary to use shell options in the .zshrc file, to manipulate how zsh handles history, so that commands are appended to .zsh_history, or are these options redundant and ignored?

I see many people recommend, when transitioning from bash to zsh, to add the following options to make history include commands from other sessions and add other functionality (that appears to be built in):

APPEND_HISTORY
HISTFILE=~/.zsh_history
HISTSIZE=100000000
SAVEHIST=100000000
setopt INC_APPEND_HISTORY
[...]

However, it seems those are built in and needn't be specified.

Bonus question: Can one list multiple shell options after a single setopt, instead of having each option in a separate setopt statement?

1 Answer 1

15

On macOS, /etc/zshrc contains (among other things) the following lines:

# Save command history
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
HISTSIZE=2000
SAVEHIST=1000

This is the reason you don't need to set those on macOS (and many other OSes): It's already done for you. However, you are of course free to override these values in your .zshrc file. Note, though, that it's recommended to make $HISTSIZE (how much history is kept in memory) at least 20% larger than $SAVEHIST (how much memory is saved to file). Read https://zsh.sourceforge.io/Doc/Release/Options.html#History for more info.

If (and only if) you are using Apple's Terminal.app, then /etc/zshrc in turn calls /etc/zshrc_Apple_Terminal. In there, we find (among other things):

# Resume Support: Save/Restore Shell State

followed by a bunch of code ported from macOS's /etc/bashrc_Apple_Terminal. In , this code is largely unnecessary, as the Z Shell can do the same with just the SHARE_HISTORY shell option.

To use Zsh's implementation instead of Apple's:

  • Add to your .zshenv file:
    SHELL_SESSIONS_DISABLE=1
    
  • Add to your .zshrc file:
    setopt SHARE_HISTORY
    
  • In your Zsh dotfiles, make sure you don't set any options incompatible with SHARE_HISTORY. Again, read https://zsh.sourceforge.io/Doc/Release/Options.html#History for more info.
  • Delete the .zsh_sessions dir created by Apple's code.

Bonus question: can we list options after setopt instead of having each one on a new line?

Yes, you can pass as many options as you like to setopt. Just try it on the command line.

1
  • What do I need to do in order to disable only the messages "Restored session" and "Saving session" etc which is getting printed on the terminal, without actually disabling anything which is happening behind the scene. Commented Sep 28, 2023 at 13:40

You must log in to answer this question.

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