A Better Zsh History, pt. 1

Configuration setup

Next: Part 2 — Options

Image for post
Image for post

I recently combed through the dot (hidden) files 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 took the opportunity to take a closer look into some 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 looking through this article in a great series over at scriptingosx.com. 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.

Setup

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

  • HISTFILE
  • SAVEHIST
  • HISTSIZE

To satisfy 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 the .zshrc we can add:

# ~/.zshrcexport 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 that 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.

# ~/.zshrcexport 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 give our shortcut seeking programmer selves just one value to maintain we can set this to 1000 and forward the value as a variable to SAVEHIST.

# ~/.zshrcexport HISTFILE="$HOME/.cache/zsh/zsh_history"
export HISTSIZE=1000
export SAVEHIST=$HISTSIZE

Recap

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

Written by

Striving for elegant solutions to complex problems.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store