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.
5 Answers
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.

- 829,060
-
-
-
Odd
zsh -f
has no discernible effect on my Amazon Linux 2 system. On the MacBook Pro (Monterey 12.3.01) it seems to just load another shell with an abbreviated command prompt. Note too that zsh on the macOS does appear to preserve command history by default but it does not on the Amazon Linux 2 system. – ScottWelker Aug 14 '22 at 15:14 -
1@ScottWelker MacOS comes with a system configuration file (
/etc/zshrc
) that sets up a few things including a slightly fancier prompt and per-terminal history.zsh -f
ignores this file so you just get the default prompt and no saved history. If you have no configuration file anyway,zsh -f
doesn't change anything compared to just runningzsh
. – Gilles 'SO- stop being evil' Aug 14 '22 at 15:22 -
Correction: On my Amazon Linux 2 system
zsh
is preserving the current shell's history. However, that history is lost across sessions, perhaps because it's a cloud instance. I expected to have my history and was surprised and confused when it was gone. – ScottWelker Aug 14 '22 at 15:32 -
1@ScottWelker As I explain in my answer, you need to tell zsh to save the history, it doesn't do it by default. MacOS has a system configuration that enables history saving, but Linux distributions usually don't. – Gilles 'SO- stop being evil' Aug 14 '22 at 17:36
-
Regarding "When you run zsh without a configuration file, it displays a configuration interface." ->
Is there a way to force this configuration setup again?
– avp Mar 20 '24 at 20:54 -
1
-
While the existing answer is correct, I thought it might be worth adding that there's possibly a better option than appendhistory
for saving your history and this is SHARE_HISTORY
.
From the docs SHARE_HISTORY
"both imports new commands from the history file, and also causes your typed commands to be appended to the history file". This means that shells are aware of each other's history as well without having to close the current one or open a new one.
So, all together you'd set it like this:
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=1000
setopt SHARE_HISTORY

- 623
-
11+1 and welcome to U/L, but personally I prefer
INC_APPEND_HISTORY_TIME
, which writes to history from all terminals as above, but that history "will not be available immediately from other instances of the shell that are using the same history file". This makes more sense to me, because then I can traverse each terminal's history independently, but it's still all logged. – Sparhawk Sep 22 '18 at 13:16 -
2Thanks! Yeah that's fair enough and I can totally see why
INC_APPEND_HISTORY_TIME
might make more logical sense. I probably shouldn't have said "better", I guess it's just a matter of personal preference at this point! – bert Sep 22 '18 at 13:19 -
1
-
setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits. – natersoz Dec 14 '22 at 17:44
If it isn't working, and you have all of this already in place, try:
fc -W
That writes the current history to the history file. And if you get:
zsh: locking failed for /home/username/.zsh_history: permission denied
then it's time to check permissions on the file and on the parent directory.

- 3,782

- 151
-
This did it for me. Using
chown
to change this file ownership solved the issue. Thanks – SubMachine Aug 12 '21 at 04:49 -
If this happens, you would most probably want to reset the permissions by the following command
diskutil resetUserPermissions / \
id -u`` – Atharva Kadlag Dec 04 '21 at 15:19 -
-
1
-
2
fc
is s shell builtin command for working with history. See https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html and search forfc
. – Chris Snyder Feb 22 '22 at 03:08 -
-
This helped me discover my instance of this issue is not as it seemed. Using a cloud-based linux system (Amazon Linux 2), I discovered my
zsh
history IS preserved in-session but lost across sessions. I have no solution for this and it may be "just how it is" with a cloud instance - researching. – ScottWelker Aug 14 '22 at 15:39 -
@vy32: POSIX says that
fc
is an abbreviation for “fix command”. O’Reilly’s Linux in a Nutshell says it “stands for either ‘find command’ or ‘fix command’ . . . since it does both jobs”. – Lucas Mar 02 '23 at 21:23
when you first switch over from bash to zsh, your bash history will not carry over. So all the history you had in bash is still in bash's history file. zsh starts capturing history from the time you start using it as your shell.
If you did not set it up to capture history, you can re-run the config wizard and tell zsh how much history to save. Either change the name of ~/.zshrc or delete it to get a new shot at the wizard.

- 181
Use alias:
alias history="history 1"
You can add this to .zprofile
To test, see the difference: history history 1
-
3That won’t help if the history isn’t being stored in the first place, as explained in the other answers. – Stephen Kitt Nov 09 '20 at 15:54
zsh
, but by defaultbash
writes to his history files upon exit, which means if you have not used it before and open some shells, they will all show now history until at least one logs out, thereby writing its history file. – DopeGhoti Sep 01 '17 at 22:34zsh
setup. – ScottWelker Aug 14 '22 at 15:34