setopt NO_HISTORY option. This prevents new commands from being added to the in-memory history list immediately. If you also want to clear the current session's history, run history -c. These two steps effectively mimic Bash's behavior of stopping recording without deleting saved files.
Understanding Zsh History Options
In Bash, users often rely on unset HISTFILE to stop writing history to disk. However, this approach has limitations in Zsh because it only affects file output, not the in-memory buffer. By default, Zsh keeps a record of every command typed during a session in RAM, regardless of whether a history file is defined. To truly "disable" history for a session, you must manipulate the shell options that control this buffering behavior.
The primary mechanism for controlling history recording in Zsh is the HISTORY option. When enabled (which is the default), Zsh appends each command to the internal history list. Disabling this option via setopt NO_HISTORY tells the shell to ignore subsequent inputs for history purposes. This is the most direct equivalent to wanting a clean slate while working in a terminal.
Step-by-Step: Temporarily Disable History
To implement this change within your current shell session, follow these concrete steps. No configuration files need to be edited, making this a safe, temporary adjustment.
- Disable In-Memory Recording: Execute the following command to turn off the history option for the current shell instance:
setopt NO_HISTORY
After running this, any command you type will not be stored in the current session's history list. You can verify this by typing some commands and then attempting to access them with the up arrow key; they should not appear.
- Clear Existing History: If you have already typed sensitive commands before disabling history, those entries remain in memory. To remove them, use the builtin
historycommand with the-cflag:
history -c
This clears the entire in-memory history list. Combining both steps ensures that no records of your recent activity are kept in the current environment. Note that this does not affect the permanent history file (typically .zsh_history) unless you also delete or truncate that file manually.
Re-enabling History
If you decide you want history functionality back, simply re-enable the option using unsetopt NO_HISTORY. However, be aware that commands typed while history was disabled are lost forever for that session. They do not retroactively appear when you re-enable the feature. If you wish to ensure that future sessions behave differently, you would need to modify your .zshrc file, but for temporary needs, the runtime options suffice.
Why Not Unset HISTFILE?
Many users coming from Bash attempt to use unset HISTFILE in Zsh, expecting it to stop all history tracking. As noted in previous drafts, this is incorrect for complete suppression. While unset HISTFILE stops Zsh from writing to the history file on exit, it does not stop the shell from keeping commands in the RAM-based history list. This means you could still accidentally recall sensitive commands using the up-arrow key or Ctrl+R search. Therefore, setopt NO_HISTORY is the superior method for privacy-focused temporary sessions.
Alternative Approaches
For users who frequently need to toggle history, creating a custom function in their .zshrc can save time. You might define a function like toggle-history that checks the current state of the option and switches it accordingly. Additionally, if you are looking to improve your overall navigation experience similar to Bash, you might find our guide on How to Make ZSH Navigation More Like Bash helpful, as better navigation can reduce the reliance on history searching for quick task repetition.
Another consideration is understanding how your shell loads configurations. Since history settings are part of the broader shell environment, knowing the Understanding the Order of Shell Source Files can help you troubleshoot why certain history behaviors persist across restarts or new terminal windows.
Frequently Asked Questions
Does this delete my permanent history file?
No, using setopt NO_HISTORY and history -c only affects the current session's in-memory buffer. Your permanent history file, usually located at ~/.zsh_history, remains untouched. If you want to delete past records, you must manually edit or truncate that specific file.
Can I make this change permanent for all new sessions?
Yes, by adding setopt NO_HISTORY to your ~/.zshrc file. However, this is generally not recommended because it disables history for every single command you type, which can hinder productivity. It is better suited for specific security-sensitive environments rather than daily driving.
What happens if I close the terminal window?
When you close the terminal, the in-memory history is discarded anyway. If history was disabled, no new entries were added, so there is nothing to write to the history file upon exit. The permanent history file will contain only the commands recorded before you disabled the option.
Is there a way to exclude specific commands from history?
Yes, Zsh allows you to set the HIST_IGNORE_SPACE option. If you prefix a command with a space, such as ls -la, it will not be saved to history even if the option is enabled. This is useful for hiding passwords or tokens without fully disabling history.
How do I check if history is currently disabled?
You can check the status of shell options by running setopt | grep NO_HISTORY. If the option is active, it will appear in the output. Alternatively, you can try recalling the last command with the up arrow; if nothing appears, history is likely disabled or empty.