Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


$ ls foo
$ echo hello
$ ls <UP_ARROW>

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

share|improve this question
up vote 9 down vote accepted

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.

share|improve this answer
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 '11 at 18:51
I edited your question to include history-search-forward (well, submitted an edit to the edit queue) – ripper234 May 27 '11 at 18:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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