Forced to use Zsh by macOS? Let’s fix our “history” command first

How to have a long history list to find previous commands easier

Rand Grey
Rand Grey
Dec 8, 2019 · 2 min read

I have a long history using the “history” command in Linux. It is like a poor man’s GitHub gist. What did I do to find a specific file? How did I compress that directory again? The history command (used with grep) means you only have to remember part of your command. For example, history 1 | grep find will give you an instant recall of all your favorite find commands.

With macOS Catalina arriving on my new laptop, I needed to setup my history settings again. I used to do this in bash, but now Apple has made the Zsh the default shell. A few things have changed.

The file /etc/zshrc contains your machine’s default settings for the history command. The following are the defaults and what I changed mine to:

# File: /etc/zshrc
# Save command history
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
HISTSIZE=2000
SAVEHIST=1000

You can see in your current terminal shell that the values match /etc/zshrc:

% echo $HISTFILE
/Users/me/.zsh_history
% echo $HISTSIZE
2000
% echo $SAVEHIST
1000

Here is what I prefer instead of the defaults. Make it big… real big:

% cat .zshrc
HISTSIZE=99999
HISTFILESIZE=999999
SAVEHIST=$HISTSIZE

You can edit your own ~/.zshrc file to add in the above 3 lines.

We’re close to having history setup. But, if you run the history command by itself, you still only see the last 16 lines. While it is interesting what I did 5 minutes ago, I often want to see what I did 5 weeks ago. I want to see something really old in history and I will follow it with a grep. What did I do to download that repo last month?

% history | grep "git clone"

Ahh, I forgot that I need to add the 1 parameter in order to start from the beginning of history. My command changes to look as follows.

% history 1 | grep "git clone"

I don’t want to always have to type the 1. The solution is to alias my history command. Add the following to your ~/.zshrc file.

alias history="history 1"

Some people don’t like to alias the default behavior of a built-in shell command, so they may shorten to “hist” as follows. I create a new command to use called “hist” that will always run history 1. Instead of the above alias, use the following:

alias hist="history 1"

So my new command for history with a grep becomes:

% hist | grep "git clone"

You now have a shorter and more powerful command than the default “history” command. It’s quicker, easier, and more seductive. 😏

Mac O’Clock

The best stories for Apple owners and enthusiasts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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