Bash history search: Partial + Ctrl-Up or Ctrl-Down

Estimated reading time of this article: 2 minutes

How to scroll in the bash command history based on partial command with Ctrl-Up or Ctrl-Down?

How to move forward/backward a word with Ctrl-Forward or Ctrl-Backward in bash command line?

I'm a longterm openSUSE user1. In openSUSE partial history scrolling or moving forward/backward a word is the default behaviour. Astonishingly, this does not work out of the box in distributions such as Debian. In the world of Docker containers, virtual machines and Raspberry Pis, I'm working often with Debian setups. There is Ctrl-R for history search, but I am not as efficient as with Ctrl-/. Thus, I miss these basic bash command line keyboard shortcuts.

To enable these keyboard shortcuts, you have to create ~/.inputrc for the current user or /etc/inputrc for all users.

# Map Ctrl-Up and Ctrl-Down to history based completion
"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward
# Map Ctrl-Forward and Ctrl-Backward to move forward or backward a word in bash
"\e[1;5C": forward-word
"\e[1;5D": backward-word

Alternatively, you can run the following command in bash.

printf '"\\e[1;5A": history-search-backward\n"\\e[1;5B": history-search-forward\n"\\e[1;5C": forward-word\n"\\e[1;5D": backward-word' >~/.inputrc

Note: The keyboard codes are terminal specific. These keyboard codes are for the xterm-256color which is the default shell terminal in current distributions.

I tested these keyboard codes on Raspbian Jessie and Debian Sid.

Partial command line search is pretty useful for Docker commands. Example

  • Type docker r and browse with Ctrl- or all your previous docker run commands.
  • Type docker e and browse with Ctrl- or all your previous docker exec commands.
  • Type n and browse with Ctrl- or all your previous nano commands.
  • Browse with plain or your last commands as before.

  1. Actually, 20 years! ↩︎