April 28, 2014

Bash shell: Incremental history searching

From using Git, Linux/Ubuntu apt-repositories, or general Bash folder navigation, you may have noticed that pushing the tab button halfway through typing an item/folder name will autocomplete it if it exists. Similarly, we can build an autocomplete feature for user commands that have been entered in the past! They are obviously stored in history somewhere, because you can already chronologically "scroll" through every single command you've issued, or just see it all by entering history. It would be nice to have a quick way to access something specific, complex or lengthy, especially if you already know how it starts.

All you need to do to find a previous command, is to start entering the first few letters of it, then you can navigate all commands you previously issued that started with your entered phrase, by using the up/down arrows! Here's how you set it up:

In terminal, open or create:
gedit  ~/.inputrc

Copy/paste the following there, and save (you may also have to close and reopen a terminal, or reload this way):
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char

And done! Now, for example, if I am now looking for the command...
sudo apt-get update

...all I need to do is enter the first few letters, something like...
sud

...and scrolling up/down through my now-selective bash history will allow me to easily locate the command in its entirety for use again!

Adapted from: Ubuntu User Community

No comments :

Post a Comment