A more convenient search in Terminal

#Bash #Linux #Mac

Terminals (specifically bash) have some mechanism that allow you to type less and avoid typos. The most common of them are using Tab to complete a command or file name and UP arrow (or Ctrl+P) to retrieve previous commands in a sequence. However, many times you want to search for a command with some character that you remember. Here is when a reverse searching comes to help. You press Ctrl+R and then as you are typing characters, it finds the first matching command in the history. You can continue your typing or press Ctrl+R to see other matching commands. Great! but how about typing a few character and then pressing UP arrow to go through all the command starting with those characters in the history? It saves you a keystroke (!) and it is much more convent (at least for me) but it is not available in a bash terminal by default .

To enable this feature you should add the following lines to your ~/.inputrc (create if not exist) :


"\e[A": history-search-backward 
"\e[B": history-search-forward
 "\e[C": forward-char 
"\e[D": backward-char 

To make the most of the command searching, it is recommended to expand your terminal history size by adding the following lines to your ~/.bashrc:

These shell-snippets are also very handy, if you have not been aware of them before: