Bash – Search bash history for already typed command

autocompletebashcommand historyshell

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?

Best Answer

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