Most of us spend our time in the terminal, running commands, configuring servers, developing tools, and more. In some instances, we find ourselves running similar commands over and over again.
Although you can create scripts to automate such tasks, sometimes the commands may vary slightly. If such a scenario happens, the command history is the best way to go.
ZSH is a popular shell built on top of bash. It stores your command history in the .zsh_history file in your home directory.
If your ZSH shell does not support command history by default, check out our zsh command history article to learn how to enable it.
The total number of commands that the ZSH command can store in the history file depends on the $SAVEHIST variable.
How to Use the ZSH Command History
Now that we know that the ZSH shell saves your commands in a file, let us identify how we can use it to make our terminal usage easier.
To view all the commands stored in your ZSH history file, use the history command.
If you run the history command with no arguments, ZSH will show all the commands stored in the history file:
Below is an example output:
55 cat .zshrc
In most cases, the history command will display an extensive list of all your executed commands. You can pipe the output to commands such as grep to search for a specific command or less to navigate it easily.
The history command also supports numerical notation. For example, to show the last n commands in the history file, use the -n where n is the number of commands to show.
For example, to show the last 5 commands, we can do:
To show from the nth command, use the +n.
For example, to show the history from the 10th command to the last one, use the command as:
How to Search ZSH Command History
Although the history command can be useful to view all your command history, it is not very useful when you want to re-run a previous command.
Let us now focus on how we can search the command history and re-run a previous command.
Up and Down Arrow
The most common way to search through the command history is to use the up and down arrow keys.
The up arrow will scroll through your command history from the latest to the earliest. Once you find the command you wish to re-run, press RETURN to execute it.
Bang (!) format
Another way to search through your command history is to use the exclamation (!) mark. If you type double bang (!!), it will run the most previous command:
The most common use case for this format is when you forget to run a command as sudo. For example, if you run the apt command as:
You can re-run the command as sudo using the syntax:
The above will re-run the apt command as sudo.
If you run !n—where n is the number of command in the command history—, you can run a specific command in the history file.
For example, to run the 12th command in the command history, you can do:
If you cannot recall the number of the command you wish to run, you can use the !word format to run the command starting with the specified word.
For example, to run the command starting with the printenv word:
The command above will run the most recent command that matches the specified word.
Another common way to search your command history is the recursive search. To use it, press CTRL + R in your terminal session. This will change your terminal session to search mode, and you can type for previous commands.
As you type, the shell will search for a matching command in the history and suggest it. To search for the next matching suggestion, press CTRL + R.
Once you find a matching command, press RETURN to execute it.
Modifying ZSH History Behavior
Although we will not go over all the tweaks you can perform to enhance the ZSH command history, the following are useful to know.
To clear your command history, use the command:
The above will give you an output indicating the history file has been cleaned.
History file deleted.
Another modification we can do is to the $HISTCONTROL variable. Check the ZSH documentation to learn more about this. However, we can tell ZSH to avoid saving duplicate commands by adding the variable as:
This tutorial discussed how to work with and use the ZSH command history to improve your terminal experience. We covered how to search the command history.
Thank you for reading!