If you are a Linux user, there is a good chance that you are using the ZSH shell. This powerful shell comes with many features, one of which is its robust history capabilities.
In this guide, we will discuss how you can access and use your ZSH history. We will also cover some best practices for managing your history files.
Let’s get started.
What are the ZSH History Variables?
There are a few variables that you can use to control your ZSH history.
- HISTSIZE – This variable sets the maximum number of commands stored in your history file. By default, this is set to 1000.
- HISTFILE – This variable sets the file name where your history is stored. By default, this is set to ~/.zsh_history.
- HISTFILESIZE – This variable sets the maximum size of the history file in bytes. By default, this is set to 2000.
How Do I Access My ZSH History?
There are a few different ways that you can access your ZSH history. The easiest way is to use the “history” command.
By default, history may return only a few items. If this is the case, run the command below:
How to Enable ZSH History?
To enable ZSH history, you need to set the HISTFILE variable. You can do this by adding the following line to your ~/.zshrc file:
You can adjust the maximum number of commands that will be stored in your history file by adding the HISTSIZE variable.
You value you specify for the HISTSIZE variable is up to you. You want to make it a large number to keep a longer history.
Zsh mailing list informs us that the maximum history size in zsh is based on LONG_MAX value from limits.h header file has an incredibly high number of 9,223,372,036,854,775,807 – enough to store quadrillions of commands. We’ll likely never reach this limit, but if remembering such a large figure seems too daunting for you, it can easily be modified to one billion without worries.
If you want to manage the size of your history file, set the HISTFILESIZE variable.
Once you have added these lines, you need to source your ~/.zshrc file. You can do this by running this command:
After sourcing your file, your ZSH history should be enabled.
How to View the ZSH History?
To view the commands stored in the ZSH history, you can run this command:
Use the up arrow key to view the earlier commands that you ran. Use the down arrow key to view the recent commands you ran.
To exit viewing your ZSH history, you can press “q“.
How to Clear the ZSH History?
Since the ZSH history is a file, you can clear it by running this command:
truncate -s 0 ~/.zsh_history
Once you view the ZSH history again, you should see it’s empty.
ZSH Shell History Not Working
You may not have set it up if your ZSH Shell History is not working. Follow the steps above to enable the ZSH history.
Another method is to check the permission of the HISTFILE:
ls -l $HISTFILE
Your user may not have read and write access to the history file. You can change the permission to your user by running this command:
chmod 600 $HISTFILE
Your user may not be the owner of the history file. Change the owner of the history file to yourself by running this command
chown user $HISTFILE
Replace <user> with the login name of your user.
Once you change the permission, your ZSH history should be working now.
In this guide, we discussed how to access and use your ZSH history. We have also covered some best practices for managing your history files.
By following the steps in this guide, you should be able to use your ZSH history more effectively.
Thanks for reading!