Skip to content

πŸ” Zsh plugin to filter out some commands from being added to you history

Notifications You must be signed in to change notification settings

MichaelAquilina/zsh-history-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

History Filter

CircleCI Version GPLv3

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.

Usage

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"

Installation

Add one of the following to your .zshrc file depending on your package manager:

ZPlug

zplug "MichaelAquilina/zsh-history-filter"

Antigen

antigen bundle "MichaelAquilina/zsh-history-filter"

Zgen

zgen load "MichaelAquilina/zsh-history-filter"

oh-my-zsh

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)

Rewrite History

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.

Contributing

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

Running Tests

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

About

πŸ” Zsh plugin to filter out some commands from being added to you history

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages