17

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.

2 Answers 2

14

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

Suppose you have just typed curl -I http://superuser.com and you forgot to type Ctrl+r first:

$ curl -I http://superuser.com

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 http://superuser.com': curl -I http://superuser.com/faq

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 http://superuser.com/faq

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:

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

Note that here we omit the single quotes.

You might find this answer useful.

4
  • 1
    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, 2012 at 14:49
  • Do you go to beginning of line first? (C-a)
    – hso
    Nov 5, 2012 at 20:15
  • 1
    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, 2012 at 3:04
  • I think it is the third time I googled this control+a control+r control+y thing! Thanks this answer is always here! :)
    – DrBeco
    Nov 25, 2017 at 21:13
2

Save four strikes:

Avoid a second Ctrl-R by adding to your .bashrc (or to your .inputrc if you prefer):

bind '"\er":"\C-a\C-r\C-y\C-r"' # alt-r = ctr-a ctr-r ctr-y ctr-r

This will map ALT-R to CTR-A CTR-R CTR-Y CTR-R

Description:

  • ALT-R: the binding key. Another good option is \C-xr (CTR-X CTR-R)
  • CTR-A: Go to the begin of the line (memorizing what is written)
  • CTR-R: Start the reverse search
  • CTR-Y: Paste what CTR-A memorized as part of the search
  • CTR-R: Triggers the reverse search with the characters so far
3
  • 1
    It still not clear to me why C-y pastes the command onto the search field (C-a is not supposed to be a "copy" command, is it?) and why C-w pastes only the first word.
    – marcus
    Jun 17, 2019 at 15:55
  • yes, C-a not only moves, but memorize (copy) the content moved over.
    – DrBeco
    Jun 19, 2019 at 2:58
  • I can't confirm that behavior (just try C-a C-y), I think it's more likely that C-r that has some special cases built in, but I don't know where they're documented.
    – marcus
    Jun 25, 2019 at 21:19

You must log in to answer this question.

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