A Better Zsh History, pt. 1

I recently combed through the dotfiles in my Mac’s home directory and was shocked to see just how polluted it had become. Now, I know that there is no real downside to leaving these files where they were, but as I prefer to keep my development environment nice and tidy, I used the opportunity to take a closer look into some Zsh configurations that I had ignored up to this point.

Zsh config files

If you are unfamiliar with the types of zsh configuration files and their uses, I recommend reading through this article in a great series over on Scripting OS X. For this example we will keep everything in the .zshrc file. If one doesn’t already exist in your home folder, in the terminal run touch .zshrc from inside of the home directory to make one.


The Zsh history mechanism only requires these first two parameters to work, but inside of the .zshrc we can configure three:


To resolve my original annoyance, let’s change the location of the history file. By default, the zsh setup wizard sets this parameter to ~/.zsh_history using HISTFILE. I prefer this to be kept with some other generated zsh specific files at ~/.cache/zsh. So in .zshrc we can add:

export HISTFILE="$HOME/.cache/zsh/zsh_history"

Note that you can name the file and the path differently than I have chosen.

Next, the SAVEHIST parameter sets the number of lines you would like saved in your history and can be set according to your machines available resources. I’ve set it to 1000 but you can decrease or increase this number until your heart is content as explained here.

export HISTFILE="$HOME/.cache/zsh/zsh_history"
export SAVEHIST=1000

The HISTSIZE parameter defines how many lines the shell will keep in one session and has a default of 30 lines. The documentation recommends a value that is no more than SAVEHIST, so to keep things DRY, we can set this to 1000 with the SAVEHIST variable.

export HISTFILE="$HOME/.cache/zsh/zsh_history"
export HISTSIZE=1000


And that’s all there is to the initial setup. We’ve defined the history file’s location using HISTFILE, specified how many lines will be saved using SAVEHIST, and how many lines the shell keeps in the session with HISTSIZE.

In part 2 we will dive into the history options that make zsh customizable to your workflow.