12

When I type ctrl+r and then start typing I can see what commands in the history match which is great.

Now is there a way to search the history on commands I have already typed in the terminal?

For example if I type ctrl+r and then type ping I can cycle through servers I have pinged. But if I type "ping" first and then hit ctrl+r it ignores the "ping" I have already typed.

Some times I'll get half way though typing out a string of commands and then think "oh I already typed this it sure would be nice to search the history on what I have already typed instead of starting over".

Does this make sense what I am asking?

2
  • ctrl-r doesn't complete the commands from history - only searches. I think what you're looking for is a ctrl-r + tab completion kind of thing. Don't think there is one.
    – jai_s
    Commented Mar 5, 2016 at 2:58
  • 1
    I've added an answer here to address the issue that I also happen to do a lot. This other stackexchange site has more tips: superuser.com/questions/384051/…
    – DrBeco
    Commented Jul 29, 2017 at 0:14

2 Answers 2

12

If you start typing a command and then, after typing some of it, remember to do a history search, you just need to:

  • CTRL+A
  • CTRL+R
  • CTRL+Y
  • CTRL+R ... (keep searching or)
  • CTRL+S ... (search in the other direction*)

Note: CTRL+S will suspend your terminal unless you explicitly revoked this behavior with

[[ $- == *i* ]] && stty -ixon

in your .bashrc


Edited: a shortcut can be seen here in this somewhat duplicated question https://superuser.com/questions/384051/is-there-a-way-of-using-ctrl-r-after-typing-part-of-command-in-bash/1271740#1271740

8

The answer is in one of the "Related" questions listed automatically by stackexchange in the right-hand column. To experiment type

bind '"\e[A": history-search-backward'

then type your partial command and use up-arrow key (which should generate escape [ A) to search for this partial input in your history. See the full answer for more.

You must log in to answer this question.

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