Aléas numériques

Linux, infosec and whatever crosses my mind.

» Partial history search using the Up key with Bash

I’m a big fan of Bash. Every time I tried another shell (especially Zsh), I always ended up using Bash at some point.

There is one cool thing with Zsh tho: when you start to enter a command and then press the Up arrow key, you go through all the previous commands in your history that start with what you typed. I find this to be way better than the traditional way of searching in the history using CTRL+R, or pressing Up repeatedly until you find what you’re looking for.

But I ended up finding how to mimic this behavior with Bash, and it’s pretty easy!

There is a special file, ~/.inputrc, that contains settings for all programs that use the GNU readline library. And Bash is one of them.

To replicate the search-with-up-arrow behavior of Zsh, you have to add the following line in this file:

  • \e[A is the raw keycode of the Up arrow key.
  • history-search-backward is the command that tells the readline lib to search backward starting at the current line and moving `up’ through the history as necessary1.

Start a new Bash instance and you’re done!

  1. man readline ↩︎