Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In bash the Ctrl+r command is very useful, I type Ctrl+r whatever and it searchs my history for commands containing the word whatever. But if I type whatever and realize that I would like search that word and hit Ctrl+r nothing happens.

Is there a way hitting a key and having it behaving as if I had typed Ctrl+r whatever instead of whatever Ctrl+r?

I have the following in my .inputrc:

"\C-p": history-search-backward

but this only works if the beginning of the line is the same.

share|improve this question

migrated from Jan 30 '12 at 18:37

This question came from our site for professional and enthusiast programmers.

up vote 9 down vote accepted

You can search bash's history using what you have already typed easily.

Suppose you have just typed curl -I and you forgot to type Ctrl+r first:

$ curl -I

If you want to do an i-search on your history, go to the beginning of line first (Ctrl+a), enter i-search (Ctrl+r) and type Ctrl+y. This should search using the contents of the whole text you already typed:

(reverse-i-search)`curl -I': curl -I

Alternatively, you can use Ctrl+w instead of Ctrl+y to search using just the first word of the text you just typed:

(reverse-i-search)`curl': curl -I

Binding it all to a single key

If you want to do all this in one keystroke, you can bind a single key to a keyboard macro. If you want to use, say, F12 run:

$ bind '"\e[24~":"\C-a\C-r\C-y"'

That will last for the session.

Making it permanent

Just define the macro in your ~/.inputrc:


Note that here we omit the single quotes.

You might find this answer useful.

share|improve this answer
I am not sure if it is a problem with my configuration, but after I type c-r typing c-y does not paste anything there. If I type c-y by itself just pastes the text, so I cannot combine the two commands... – skeept Nov 5 '12 at 14:49
Do you go to beginning of line first? (C-a) – hso Nov 5 '12 at 20:15
You are right, I wasn't going to beginning of line. Now I have in my .inputrc "\C-xr": "\C-a\C-r\C-y" and it works very nice! Thanks for this answer. – skeept Nov 6 '12 at 3:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .