4

This question already has an answer here:

It's normal when using terminals that pressing up and down cycles through previous commands entered chronologically.

I learned from a site how to add to that behaviour by autocompleting what I'm typing at the cursor based on previously entered commands when pressing up and down.

For example, let's say I typed the following commands in this order:

  1. ls Videos
  2. ls Vids
  3. lsomething
  4. ls Videos/Funny/Old
  5. vi file
  6. ls Music

If I haven't typed anything, pressing up and down will cycle through all of the lines just like normal. But, if I've typed ls, pressing up and down cycles through all commands where the first 2 characters are ls (lines 1,2,3,4,6 above). Entering ls (with a space after) though, will only let up and down cycle through lines 1,2,4,6. Similarly, ls Vid lets the arrows cycle through lines 1,2,4 but ls Videos just allows cycling between lines 1 and 4.

It's pretty useful to complete values specific to commands that you need to enter over and over again without having to cycle through every single unrelated command you've entered between them. For example, I'm trying to test the effects of env values, like locale, on things:

env [many different values] [command]
[insert many different commands here]
env [slightly different values from before] [command]
[doing other stuff]

I'd only have to press up a few times after typing env to cycle between the values I've tried.

I apologize if it's a bit of a long way to describe it. I don't know what it's called. My terminals used to have this behaviour, but they seem to have lost it and I can't find the site I learned it from. I think it has something to do with editing ~/.bashrc.

marked as duplicate by αғsнιη, Eric Carvalho, muru, Charles Green, Seth Oct 31 '14 at 0:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You can search backward and forward in your history. – Cyrus Oct 25 '14 at 7:22
3

To get the behavior you are after, add these lines to your ~/.inputrc (create the file if it does not exist):

"\e[A": history-search-backward
"\e[B": history-search-forward

Then, open a new terminal and it will work as expected. This behavior is controlled by the settings in /etc/inputrc and ~/.inputrc. You can read more about them in the Readline Command Names section of man bash:

   non-incremental-forward-search-history (M-n)
          Search forward  through  the  history  using  a  non-incremental
          search for a string supplied by the user.
   history-search-forward
          Search  forward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.

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