user
How to navigate in bash history for commands that start with a specific prefix using the up / down keys?
alphonsio

Ctrl + R


To navigate Bash history for commands that start with a specific prefix using the up/down arrow keys, you can use two options :

Option 1. Reverse Search

You can always use Ctrl + R , then type part of the command — it will show the most recent matching one.

AFter hitting Ctrl + R, you'll get a new prompt (reverse-i-search): that will filter command history :

(reverse-i-search)`': 

Option 2. Prefix Search with Arrow Keys

To enable prefix-based navigation (like typing git then pressing ↑ to only see commands that started with git), you need to use a feature called reverse-search history with a prefix.

Add this to your ~/.inputrc

# Enable up/down arrow to search bash history by prefix
"\e[A": history-search-backward
"\e[B": history-search-forward

Then either:

bind -f ~/.inputrc  # Apply immediately

Or restart your terminal.

How it works after setup:

  1. Type the beginning of a command (e.g., sudo).
  2. Press → It will show the last command that starts with sudo.
  3. Press again → See earlier matching commands.