Vim-like history in bash?

5

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.

naught101

Posted 2012-10-23T00:49:59.933

Reputation: 911

1Worth nothing that zsh contains exactly this functionality. – naught101 – 2014-07-24T00:59:06.583

Answers

4

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

Steve

Posted 2012-10-23T00:49:59.933

Reputation: 511

As I said, I am aware of the history search function. That's not what I'm looking for here. – naught101 – 2012-10-23T01:33:08.150

@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 – 2012-10-23T01:47:33.377

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 – 2012-10-23T02:11:10.503

@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 – 2012-10-23T02:18:07.863

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 – 2012-10-23T02:21:44.037

@naught101: Glad we got it working :-) Yes, using the up/down keys could be problematic (i.e. users may still want the option of history scrollback). You're right the ~/.inputrc file could be modified instead. But some users may not use it. I don't for example. – Steve – 2012-10-23T02:26:28.087

3

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.

Haru

Posted 2012-10-23T00:49:59.933

Reputation: 103