Scott's Recipes Logo

Giving zsh A Global History

Pizza courtesy of Pizza for Ukraine!

Donate Now to Pizza for Ukraine

 

Last Updated On: 2025-09-01 04:31:52 -0400

Disclaimer: I stole most of this from Markus Wein here but added an important note for hard core shell users. And by hard core shell users, I mean someone like myself who uses multiple, concurrent shell terminals.

A shell’s history gives you access to your past commands. An extensive history means that anything you once did – that crazy sed command – is just a quick search away:

history | grep sed

The default for almost all shells is that each shell has its own history and the one that saves last overwrites all others. This is, I’m sorry, monstrously idiotic in a multitasking operating system like, say *nix, WHERE ALL SHELLS EVER HAVE EXISTED. What were they thinking?

Here is how to turn this in on your shell:

  1. Open the file ~/.zshrc in your editor.
  2. Add these commands to the bottom of the file:

    # Appends every command to the history file once it is executed setopt inc_append_history # Reloads the history whenever you use it setopt share_history

  3. And, most importantly, go into every single shell window you have open and type the command below and press enter:

    source ~/.zshrc

This tells that shell window to reload the shell configuration file and start saving your history. If you don’t do this then when that shell window becomes active, it will hijack the history and save its current history over your global history.