Bash – Search for a previous command with the same prefix when I press Up at a shell prompt

bashcommand historyshelltcshzsh

Coming from a FreeBSD world I wish to make the Linux terminal behave like FreeBSD one, especially the 9.1 version, basically when you type cd in the terminal and push the "up" arrow you can browse all the commands in the history starting with cd which makes you gain a lot time.

I don't know how to enable this feature in Linux Debian or CentOS which force me to type the whole, could someone please help.

Best Answer

Add to the following to ~/.inputrc:

# Press up-arrow for previous matching command
"\e[A":history-search-backward
# Press down-arrow for next matching command
"\e[B":history-search-forward

Explanation

~/.inputrc is the configuration file for GNU readline. Many shells, including bash and tcsh use readline for command line editing. The two lines above will tell readline to invoke its history search functionality when the escape sequences for the up-arrow key (\e[A) and down-arrow key (\e[B) are encountered.

Related Question