How to filter zsh and bash commands


Borislav Hadzhiev

Sun Dec 27 20201 min read

How to start typing a command and go through the history of commands you've previously typed that match the filter

Many people know about the shell functionality that when you press the up or down arrow keys you navigate back and forth between commands you've previously used.

However there's a couple of lines you can add to your bashrc or zshrc file to improve this functionality - when you start typing a command i.e. aws and press the arrow keys you only navigate between commands you've previously used that start with aws

In order to enable this, in your .bashrc add the following lines:

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

Alternatively if you use zsh add the following lines to your .zshrc:

bindkey '\eOA' history-beginning-search-backward
bindkey '\e[A' history-beginning-search-backward
bindkey '\eOB' history-beginning-search-forward
bindkey '\e[B' history-beginning-search-forward

Source your shell so the updated config takes effect, i.e.:

source ~/.bashrc


source ~/.zshrc

and start filtering your command history based on input. Start typing a command i.e. cd and press the up and down arrows to navigate between previously typed in commands that match the filter cd.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee