History isn’t preserved in zsh

command historyzsh

Whenever I open a new instance of a terminal, the history is empty. Why is that? Do I need to set something up? In bash there's no need for this, though.

Best Answer

Bash and zsh have different defaults. Zsh doesn't save the history to a file by default.

When you run zsh without a configuration file, it displays a configuration interface. In this configuration interface, select

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Recommended.)

then review the proposed settings and select

# (0)  Remember edits and return to main menu (does not save file yet)

Repeat for the other submenus for (2) completion, (3) keybindings and (4) options, then select

(0)  Exit, saving the new settings.  They will take effect immediately.

from the main menu.

The recommended history-related settings are

HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
setopt appendhistory

I would use a different name for the history file, to indicate it's zsh's history file. And 1000 lines can be increased on a modern system.

HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt appendhistory

These lines go into ~/.zshrc, by the way.

Related Question