Unify Your macOS Terminal Command History

brockwalters
New Contributor II

I'm embarrassed to not have known this until now, but, the joy in the discovery outweighs the pain of embarrassment, so, here goes.

How do you feel about having your Terminal history split into unique sessions in the .bash_sessions or .zsh_sessions file? Do you hate it? Does it drive you mad with rage that you can't get your entire command history in one place? Just me?

You can fix it.

1) Close any open Terminal windows other than the one you are working in.

2) Run the following command to delete the sessions folder in your home folder:

rm -rf .zsh_sessions

This is fine. NOTE: if your default shell is still bash it will be called

.bash_sessions

3) Run the following command:

touch ~/.zsh_sessions_disable

If your default shell is still bash use

touch ~/.bash_sessions_disable

4) Quit Terminal & open it again.

That's it. Now your Terminal command history is unified into the .zsh_history or .bash_history file (just like the days of yore...) If you want the sessions behavior back, delete the sessions_disable file. So simple.

The end.

Enjoy!

P.S. This is a bit recursive (because the link below references THIS POST...) but I wanted to update here with an additional configuration step:

add

export SHELL_SESSIONS_DISABLE=1

to your .zprofile on macOS Big Sur & later to prevent the sessions folder from recreating itself in every new shell / Terminal window.

See the post at this superuser.com link:

Disable zsh session folder completely

for the full discussion!

4 REPLIES 4

bradtchapman
Valued Contributor

And for those of you who didn't know: zsh does not save a history by default.

Follow these instructions to create a history file.

dlondon
Valued Contributor

That's odd because I'm looking at a clean installation of Catalina and all I see in my home directory is .zsh_history - no .zsh_sessions file

brockwalters
New Contributor II

@bradtchapman it may be the case for zsh generally that "zsh does not save a history by default" but I don't think that's the case on macOS. On a brand-spanking new install of macOS with no command history after executing a few commands in Terminal & not doing anything else special the .zsh_history file was generated in my home folder automatically.

arminBriegel
New Contributor III

The .bash_sessions file is not to be confused with your .bash_history. The bash_history keeps a log of each command you enter in the shell. It is a feature of the bash shell.

There is an equivalent in zsh, and as @dlondon has pointed out, it history is not usually enabled in zsh by default. However, in macOS Catalina, Apple changed the /etc/zshrc file to enable ZSH history by default. Though there are a few additional zsh history settings you probably want to configure as well.

The bash session state you mention, is the behavior that when you quite an reopen Terminal and bash is set as your default shell, the all the Terminal windows and their state (as in, the previous session is shown as greyed out text before the prompt) are restored.

This feature does not work when zsh (or any shell other than bash) is set as the default shell.

@brockwalters instructions are the correct instructions to disable the session state restore in Terminal for bash, but the stored session state is independent of the bash command history.

If you want a shared bash_history between terminal windows, there are a few approaches.

You can point both zsh and bash to use the same history file, and as long as you don't use the extended history file (which stores additional metadata, such as a timestamp) it could work...