Autocompletion

How to Enable History Search in the Terminal

When working in a terminal, navigating through command history efficiently can save time. By modifying the .inputrc file or .bashrc, you can enable a feature that allows you to search command history using the arrow keys.

Step 1: Modify the .inputrc File

Run the following command to append the necessary configuration to your ~/.inputrc file:

cat >> ~/.inputrc <<'EOF'
"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on
EOF

Explanation:

  • \e[A and \e[B correspond to the Up and Down arrow keys.

  • history-search-backward allows searching for commands in history that match the current input when pressing the Up arrow.

  • history-search-forward does the same but moves forward in history when pressing the Down arrow.

Step 2: Apply the Changes

To apply the changes, either open a new shell session or reload the .inputrc file manually:

bind -f ~/.inputrc

Step 3: Test the Feature

  1. Open a new terminal session.

  2. Type a few characters of a previously executed command.

  3. Press the Up arrow (↑) to cycle through past commands that match your input.

  4. Press the Down arrow (↓) to move forward in the search.

Last updated