zsh

How Do You Check ZSH History

As Linux power users, we often come across instances where we need to rerun specific commands. Although some are simple commands that we can retype, some are complex and messy regular expression queries that will take time to reconstruct.

In such scenarios, we can use our shell command history to scroll through our previous commands or use the reverse search to search for a specific command using the up and down arrows.

This tutorial will discuss ways to find and reuse our previous commands using the ZSH command history feature.

ZSH History Variables

Before we dive into how to search through our command history in ZSH, it is helpful to discuss various variables that ZSH uses to handle command history.

In some cases, you may find that the command history for the ZSH shell is not stored in a persistent file. Therefore, some of the variables we will discuss might not be set.

The essential variables you should be aware of when working with ZSH history are:

$HISTFILE – This variable describes the path to the ZSH history file. If you have a framework such as oh-my-zsh installed, this will refer to the .zsh_history file in your home directory.

echo $HISTFILE                                                                                                                                                                                                                
/home/ubuntu/.zsh_history

$HISTSIZE – The HISTSIZE variable determines the number of commands loaded into the memory from the history file. By default, this value is set to 100 commands.

echo $HISTSIZE
1000

$SAVEHIST – Another essential variable is the SAVEHIST. It determines the maximum number of commands stored in the .zsh_history file. The default value for this variable is similar to $HISTSIZE.

echo $SAVEHIST
1000

The above are some key ZSH history variables you should know. You can learn more by checking the documentation or using the command:

man zshoptions

How to Enable ZSH History

As mentioned earlier, you may encounter a situation where ZSH does not retain your command history. To resolve this, you need to add the $HISTFILE to your .zshrc file.

Add the following entry to your .zshrc file.

export HISTFILE=~/.zsh_history

You can also add the above environment variables to specify the maximum number of commands stored in the history file. Feel free to change this value to any number you wish. However, unlike bash, you cannot add a 0 to store unlimited commands.

export HISTSIZE=1000
export SAVEHIST=1000

To save the changes to your current session, use the command:

source ~/.zshrc

How to View and Use ZSH History

Now that we have enabled command history in the ZSH shell, we can learn how to use it.

All the commands you type in your ZSH session will be automatically stored in the history file for reuse.

To view all the commands stored in the .zsh_history file, you can cat the contents of the file as:

However, just viewing all the commands, you have previously executed doesn’t help us.

To review all the commands you have executed previously, use the up and down arrow to scroll through the history.

The up arrow will scroll up the file showing the earliest command you ran, while the down arrow will scroll down the file showing the latest command in your history.

When you find the command you wish to rerun, press enter to prompt the shell to execute it.

If you come across a command with a similar syntax to what you want but not an exact one, you can use the left and right arrows to navigate through the command and edit it.

TIP: To quickly navigate to the beginning or end of the command, use the CTRL + A and CTRL + E, respectively.

Conclusion

This tutorial showed you how to work with ZSH command history. If you do not need to manage your ZSH command parameters manually, consider installing the oh-my-zsh framework that comes with lots of features enabled out of the box.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list