Commands typed with a leading space in the terminal are often used to avoid saving sensitive or experimental commands in your shell history. By default, Zsh on macOS supports this privacy feature, but it may require configuration to ensure these commands are truly excluded from both .zsh_history and .zsh_sessions files. This guide explains how to configure your Zsh environment so that commands with a leading space are not recorded, keeping your command history confidential and uncluttered.

Configure Zsh to Ignore Commands With Leading Space

Step 1: Open your terminal and edit your Zsh configuration file. The configuration file is usually ~/.zshrc. You can use nano or your preferred text editor:

nano ~/.zshrc

Step 2: Locate or add the HIST_IGNORE_SPACE option. This Zsh option tells the shell to skip saving any command that starts with a space. Add the following line if it’s not already present:

setopt HIST_IGNORE_SPACE

This ensures that any command you type with a leading space will not be saved in your .zsh_history file.

Step 3: Save your changes and reload your Zsh configuration to apply the new setting. You can do this by either closing and reopening your terminal or running:

source ~/.zshrc

Step 4: Test the configuration. Type a command with a leading space, such as:

 echo "this is a test"

Then, check your .zsh_history file to confirm the command was not recorded:

tail ~/.zsh_history

Prevent Commands With Leading Space From Being Saved in .zsh_sessions

Zsh sessions, managed by plugins like zsh-sessions or Oh My Zsh’s session features, may also store recent commands. These session managers sometimes ignore the HIST_IGNORE_SPACE option and save all commands, including those with leading spaces. To prevent this, you may need to adjust your session manager’s configuration or update the plugin.

Step 1: Check your .zsh_sessions file, usually located in your home directory or a plugin-specific folder. Open it with a text editor to see if commands with leading spaces are being saved.

Step 2: If you use a session plugin (such as zsh-sessions), review its documentation or configuration files for options related to command history filtering. Some plugins offer settings to respect HIST_IGNORE_SPACE or to exclude commands with a leading space.

Step 3: If the plugin does not support this filtering, consider updating to the latest version or switching to a session manager that respects the HIST_IGNORE_SPACE setting. Alternatively, you can manually clear sensitive lines from .zsh_sessions after your session ends.

Step 4: After making changes, test by starting a new session, entering a command with a leading space, and checking the .zsh_sessions file to verify it is not recorded.


Alternative Approach: Use HISTCONTROL for Bash Compatibility

If you frequently switch between Bash and Zsh, you may be familiar with the HISTCONTROL variable in Bash. While Zsh does not use HISTCONTROL, you can add it to your ~/.zshrc for compatibility, although the preferred method is to use setopt HIST_IGNORE_SPACE:

export HISTCONTROL=ignorespace

This approach may help if you use tools or scripts that check for HISTCONTROL, but it does not replace the Zsh-specific option.


With these settings, your Zsh environment on macOS will skip saving commands with a leading space in both history and session files, keeping your sensitive commands private and your logs uncluttered.