6

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* ]]
then
    bind '"\e[A": history-search-backward'
    bind '"\e[B": history-search-forward'
fi

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.

Thanks!

0

1 Answer 1

4

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

2
  • 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? Commented Sep 4, 2015 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. Commented Sep 4, 2015 at 22:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .