zsh plugin that allows you to specify patterns that will automatically exclude commands from being inserted into your permanent history. Particularly useful for preventing secrets being written.
After installation, set the environment variable HISTORY_FILTER_EXCLUDE
to an
array of keywords that will be used to determine when to filter out the
command from history.
For example:
export HISTORY_FILTER_EXCLUDE=("_KEY" "Bearer")
Now every time any of the keywords that are contained in ZSH_HISTORY_EXCLUDE
are in a command
you execute, the command will be written to internal history but be prevented from being written
to your permanant history file (i.e. your HISTFILE
).
In practice what this means is that the command will still be available when you
press the up and down keys on your keyboard and the history
command - but only
within the same shell session. As soon as you open another terminal you will find that
the command does not exist in your history.
For example, using the HISTORY_FILTER_EXCLUDE
value above -
all of the commands below will be filtered out from permanant history.
curl -H "Authorization: Bearer SEKRETTOKEN" https://api.example.com export SOME_DEVELOPER_KEY="SEKRETKEYVALUE"
Add one of the following to your .zshrc
file depending on your
package manager:
zplug "MichaelAquilina/zsh-history-filter"
antigen bundle "MichaelAquilina/zsh-history-filter"
zgen load "MichaelAquilina/zsh-history-filter"
Copy this repository to $ZSH_CUSTOM/custom/plugins
, where $ZSH_CUSTOM
is the directory with custom plugins of oh-my-zsh (read more):
git clone https://github.com/MichaelAquilina/zsh-history-filter.git $ZSH_CUSTOM/plugins/zsh-history-filter
Then add this line to your .zshrc
plugins=(zsh-history-filter $plugins)
Once you install this plugin and specify the patterns you would like to exclude from history, you might want to retro-actively apply the the filter to your currently stored history (for e.g. to remove secrets from being viewable to anyone who has read access to your history file).
You can do this very simply by running the rewrite_history
command.
Pull requests and Feedback are welcome! π
I have tried to cater for as many use cases that I can think of. However, they are naturally tailored to to my own workflow and I could be missing many others.
Because of this if there is a specific use case that does not work as you would expect or if you have any suggestions to how the plugin should behave, feel free to open an issue
Install zunit. Run zunit
in the root
directory of the repo.
$ zunit Launching ZUnit ZUnit: 0.8.2 ZSH: zsh 5.6.2 (x86_64-suse-linux-gnu) β version exported β hooks are loaded by default β no excludes β excludes correctly β excludes correctly - 2 5 tests run in 135ms Results β Passed 5 β Failed 0 βΌ Errors 0 β Skipped 0 βΌ Warnings 0
NOTE: It is required that you use a minimum zunit version of 0.8.2