Following the instructions here, I edited by .bash_profile to enable the up and down arrows to autocomplete what I have typed in the shell based on previous commands.

I added to my .bash_profile:

# bind history search to up and down arrows
if [[ $- == *i* ]]
    bind '"\e[A": history-search-backward'
    bind '"\e[B": history-search-forward'

This is great, but I want the down arrow to return to my original text if I can't find the command I'm looking for.

For example, say I have three previous commands:

  • ls -l
  • ls -al

Now, I type ls with the intention of autocompleting it to ls -a.

I hit the up arrow, which (with my current implementation) autocompletes my text to ls -al. I hit the up arrow again, which autocompletes my text to ls -l. At this point, I decide that I won't find the command I'm looking for. If I press the down arrow, the text changes to ls -al, but pressing the down arrow again doesn't change anything. I want pressing the down arrow to return to my original text (ls in this case) after I've reached the most recent command in the history.

I would really appreciate it if anyone could help me out or point me in the right direction.



Ctrl+K will kill the text from the cursor to the end of line. so If you haven't moved the cursor, Ctrl+K will give you (ie, effectively revert to) your original text

| improve this answer | |
  • Is there any way to set it up that down arrow maps to history-search-forward unless there is nothing forward, in which case the down arrow would map to Ctrl+K? – Zach Kirsch Sep 4 '15 at 3:18
  • @ZachKirsch You can bind a key to a bash function which checks the line content. See unix.stackexchange.com/questions/150578/… for an example. It's cumbersome to call a readline function afterwards, I think it would be simpler to call fc to retrieve history items. Mixing shell functions and line editor commands is easier in zsh. – Gilles 'SO- stop being evil' Sep 4 '15 at 22:21

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.