Search bash history for already typed command

8,932

Solution 1

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

Solution 2

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.

Share:
8,932

Related videos on Youtube

user1028270
Author by

user1028270

Updated on September 18, 2022

Comments

  • user1028270
    user1028270 over 1 year

    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?

    • Admin
      Admin about 8 years
      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.
    • Admin
      Admin almost 7 years
      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/…