Forced to use Zsh by macOS? Let’s fix our “history” command first
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.
/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
You can see in your current terminal shell that the values match
% echo $HISTFILE
/Users/me/.zsh_history% echo $HISTSIZE
2000% echo $SAVEHIST
Here is what I prefer instead of the defaults. Make it big… real big:
% cat .zshrc
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
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. 😏