Home > macOS, Unix > Zsh history command doesn’t show all history entries by default

Zsh history command doesn’t show all history entries by default

Starting with macOS Catalina, Apple’s default shell for the Terminal changed from bash to zsh. As part of that change, the behavior of the history command changed.

Bash:

Running the history command lists all history entries.

Zsh:

Running the history command lists only the 15 most recent history entries.

To see more history entries, you can pass a numerical value to the history command. However, the bash and zsh shells handle this differently also.

Bash:

history (number_goes_here): Shows the most recent entries as defined by the number.

For example, the command shown below will show the fifteen most recent entries:

history 15

Zsh:

history (number_goes_here): Shows all entries which follow number_goes_here.

For example, the following command will show all history entries after history entry #1 (which will include all history entries):

history 1

history -(number_goes_here): Shows the most recent entries as defined by the number.

For example, the command shown below will show the fifteen most recent entries:

history -15

For those interested in why this behavior is different, I recommend reading the zshbuiltins manpage’s section on the fc command. This can be accessed by running the following command:

man zshbuiltins

The reason is that in the zsh shell, the history command’s mechanism is using the following built-in function:

fc

Screen Shot 2022 01 29 at 9 58 36 PM

For my own needs, I wanted to recreate bash‘s default behavior for the history command in the zsh shell. The way I chose to do this was to add the following line to the .zshrc file in my home folder.


alias history='history 1'

view raw

.zshrc

hosted with ❤ by GitHub

Screen Shot 2022 01 29 at 8 50 22 PM

The .zshrc file is the configuration file for the zsh shell and is used to customize the zsh shell’s behavior. Adding this line to the .zshrc file will run the following command whenever the history command is invoked:

history 1

The downside to this approach is that it interferes with the original functionality of the history command. An alternative approach would be to add the following line to the .zshrc file:


alias fullhistory='history 1'

view raw

.zshrc

hosted with ❤ by GitHub

Screen Shot 2022 01 29 at 9 11 05 PM

Now all history entries will be displayed when the following command is entered:

fullhistory

Categories: macOS, Unix
  1. Bennett Mead
    May 2, 2022 at 8:46 pm

    Thank you so much for this! As a bash convert, I was getting super confused about why the history command was not listing the full number of commands after setting HISTSIZE. I think there will now be some other shortcut aliases too, since you can now list in reverse too…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: