In bash, it's possible to scroll through the command history using the up and down keys. In vim, this is also possible (after pressing :). But with vim, if you type the first few letters of a previous command, the up/down cycle only through commands that start with those letters. This would be massively useful in bash, especially simple for commands like cd, and vim itself. Is there a way to get this behaviour in bash?

I am aware of the history search function in bash, but it's no-where near as easy and quick to use.

  • 1
    Worth nothing that zsh contains exactly this functionality. – naught101 Jul 24 '14 at 0:59

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


Put the following in your /etc/inputrc file:

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

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

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


Source your console:

. ~/.bashrc


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.


This answer is a summary of the information taken from here

  • As I said, I am aware of the history search function. That's not what I'm looking for here. – naught101 Oct 23 '12 at 1:33
  • @naught101: The method I describe accomplishes exactly what you describe. But instead of using the up/down keys, you use alt-p/n. If this is not want you want, you should consider editing your question. – Steve Oct 23 '12 at 1:47
  • It doesn't work at all for me, actually. If I type 'vim', and hit Alt+P, the word 'vim' is replaced with a colon. If I hit alt+p again, I just get ^[p added after the colon... (on kubuntu 12.04) – naught101 Oct 23 '12 at 2:11
  • @naught101: Did you remember to source your terminal? See step 2. Alternatively, close your terminals and open up a new one. It should then work for you. HTH. – Steve Oct 23 '12 at 2:18
  • ok, yes I had source'd, but it seems to require a restart. Yes, this is more or less that I wanted, but it would be better on the up/down keys. Perhaps that is a bad idea for some reason though.. Also, you can do this in ~/.inputrc, which is probably a safer thing to suggest. – naught101 Oct 23 '12 at 2:21

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.