Vim-like history in bash?

Solution 1:

Here's one way to set up history-search-backward and history-search-forward:

Step1:

Put the following in your /etc/inputrc file:

$if mode=emacs
"\ep": history-search-backward
"\en": history-search-forward
$endif

(Or simply put the following between in the existing if statement)

"\ep": history-search-backward
"\en": history-search-forward

Step2:

Source your console:

. ~/.bashrc

Step3:

Test it out. For example, to search backwards through all your vim commands, type:

vim Alt+P

And keep hitting Alt+P, to loop through all of your commands.


EDIT:

This answer is a summary of the information taken from here

Solution 2:

Not exactly what you are after - but I always use 'set -o vi' in the .bashrc or .profile.

To search previous history type esc (to get you out of edit mode), followed by a forward slash and your query. Then press enter to search. You can then use 'n' or 'N' to go forwards and backwards through the matching queries. If you are used to vi this might be a solution.

It will mean that the command line becomes like vi though - for example capital 'A' will take you to the end of the line and put you in edit mode. It can be useful, but it can also become confusing if you find yourself typing when not in edit mode. If you do not want this than another solution would be better.