Zsh history across sessions

 · 2 min · torgeir

You can customize when zsh persists and reads its history using the options share_history, append_history and inc_append_history.

Terminal Zsh Session

For a long time I have been wondering when zsh actually persist its history. Or rather, why I can’t recover the previous command I just typed in one shell in another shell, directly after. A colleague suggested its written when a session ends, which turns out to be correct.

Something about this always felt off for me, but I never bother looking into it.

Today I discovered share_history, append_history and inc_append_history. It seems I have had append_history in my .zshrc for a long time.. Indeed, it appends history to make them available in other shells, but only once a shell is exited. And its only read in newly spawned shells. append_history appends to the history file, instead of replacing it as shells exists (in the order of the exited shells).

inc_append_history on the other hand, incrementally appends history as commands are finished in the current session. The written history is still only read on newly spawned shells. share_history is like setting inc_append_history, only it also reads the history on each command.

I usually hit m-p or m-n to skip through the partially matching history items anyways, using the zsh-history-substring-search plugin’s history-substring-search-up and history-substring-search-down, so I don’t really notice the added noise of repeated or unwanted commands that is now be shared.

Feels much more natural.