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.
$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.
$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.
The above are some key ZSH history variables you should know. You can learn more by checking the documentation or using the command:
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.
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.
To save the changes to your current session, use the command:
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.
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.