macOS .zsh_sessions, .zsh_history, and `setopt APPEND_HISTORY`
On macOS, there is a sessions directory that combines commands from multiple zsh sessions into a single history file.
Is it necessary to use zsh 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?
Solution 1:
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: 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
at least 20% larger than $SAVEHIST
. 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 zsh, this code is largely unnecessary, as the Z Shell can do the same with just setopt sharehistory
.
To use Zsh's implementation instead of Apple's:
- Add to your
.zshenv
file:SHELL_SESSIONS_DISABLE=1
- Add to your
.zshrc
file:setopt sharehistory
- 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.