First, by default, zsh doesn't save the command history. You need to set HISTFILE
to the path to the file where you want to save the history and SAVEHIST
to the number of lines you want to save. See Command history in Zsh. In the rest of this answer, I'll assume that the history is being saved, and discuss when it gets saved and loaded.
By default, zsh loads the history when it starts and saves it when it exits. When it saves the history, it appends the history of the current session to the history file. So no history is lost, even when there are concurrent sessions (until the history file reaches 1.2 times $SAVEHIST
lines, at which point zsh will trim the file). The main advantage of this approach is that lines from a session remain grouped together. A downside is that lines from session A will only appear in sessions started after session A exits. Another downside is that if your machine (or zsh) crashes, the history is lost (but the history is saved if the terminal merely disappears, for example because an SSH connection dies due to a network problem).
If you turn on the share_history
option, zsh will save each history line as soon as it's entered, and will load new history lines as soon as they become available. (Or more precisely, before displaying a new prompt; zsh won't read history while you're editing a command line.) The main advantage is that history lines are available immediately in all sessions. The main downside is that lines from different sessions will end up mixed, so lines that are consecutive in the history might not make any sense with respect to each other.
There are also the options inc_append_history
and inc_append_history_time
. They cause zsh to save the history as soon as it runs each command, but it won't read new history lines automatically (other than when it starts, of course). The _time
variant saves the history after running a command, rather than before, which allows zsh to save the time it took to run.
You can turn off the append_history
option, and zsh will overwrite the history file when it saves it, instead of appending to it. As a consequence, if you start session A, then start session B, then exit A, then exit B, the history from A will be lost. I can't think of a good reason for choosing this behavior (but it's the bash default).
You can import new lines the history file at any time by running fc -RI
. This is useful if you don't use share_history
, to explicitly import the history from other sessions from time to time.
You can save new lines to the history at any time by running fc -AI
. This is useful if you don't use inc_append_history
or share_history
, to explicitly export the current session's history from time to time.
What I described as defaults in this answer are the zsh defaults. Some distributions and most zsh configuration frameworks will at least enable the history by default, and may change some history-related options.
append_history
option? The optionsinc_append_history
andshare_history
make the order more real-time. See also unix.stackexchange.com/questions/111718/command-history-in-zshshared_history
enabled. I think I actually don't want shared_history... I just want to make sure I don't lose anything.