Fixing macOS zsh Terminal History Settings

Related thread on HN: https://news.ycombinator.com/item?id=33186412

The Problem

macOS ships with Z shell settings that do not find commands far back enough using the history command to be useful.

If you have a shell open and echo "test", then history | grep echo, it will return something like: 891 echo "test".

However, if you issue 16 more unique commands, history | grep echo will no longer be able to find your 891 echo "test" entry.

This is because running the history command only lists the 16 last unique commands. These relatively recent history entries don’t reflect a real world use case of the history command where you want to remember a complex command you used a week or a month ago.

macOS stores the main configuration file in /etc/zshrc.

You should not edit the one in /etc.

To change your zsh settings, you need to make one in your home directory instead.

This is because the one in /etc usually gets blown away during updates and if you mess up configuration settings in this file, it can be difficult to fix.

The Solution

To make your own .zshrc file that will persist through updates and search through your entire history file:

  1. nano ~/.zshrc
  2. Add the following lines to your new .zshrc file:
alias history="history 1"
HISTSIZE=99999
SAVEHIST=$HISTSIZE
  1. Save

This ensures that history command can display commands back to the beginning of your history file, rather than the last 16 commands.