9

How can I make pressing up/down on bash bring up the last command that started with the prefix I already entered?

E.g.

$ ls foo
$ echo hello
$ ls <UP_ARROW>

Clicking UP after entering ls should bring up "ls foo" and not "echo hello"

1 Answer 1

12

A few possibilities:

  1. Bind the history-search-backward/forward commands. I believe this is closest to what you want:

    bind '"\e[A":history-search-backward'
    bind '"\e[B":history-search-forward'
    
  2. Use the history i-search. This is probably bound to ^R. On an empty line, ^Rls<space> will get you back to ls foo, then ^R again will take you to the last ls command (or other command with ls somewhere in the line) before ls foo, etc...

  3. Bind the magic-space command: bind SPACE:magic-space. Then type (on an empty line) !ls<space>, and readline will perform history substitution automatically, leaving you with ls foo.

2
  • 1
    Thanks. I took option 1, and with the help of this old answer, realized that \e[B will bind the down arrow.
    – ripper234
    May 27, 2011 at 18:51
  • I edited your question to include history-search-forward (well, submitted an edit to the edit queue)
    – ripper234
    May 27, 2011 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.