40

I once found an amazing bash option, but now I can't remember how to re-enable it.

It extended autocomplete to look at your most recent history. So if you'd previously typed open index.html and then open map.html, typing

$ op

and pressing Tab once would autocomplete to open map.html. Pressing Tab again would autocomplete to open index.html.

Does anyone know how I can re-enable this?

1

3 Answers 3

35

Run in your bash:

cat >> ~/.inputrc <<'EOF'
"\e[A": history-search-backward
"\e[B": history-search-forward
EOF

Open a new shell session, or just reload inputrc:

bind -f  ~/.inputrc

Now use and after entering the beginning of the command - it will auto-complete from history.

2
  • Note: this broke by "c" key in terminals on OSX for me. Dec 2, 2019 at 16:46
  • thank you so much, been looking for this for some time now :) Jan 25, 2022 at 0:33
11

According to this page ("Turn on Bash Smart Completion") on Ubuntu Blog, it is as easy as editing your bash.bashrc file. For clarity, I rewrote these instructions below in a more beginner-friendly manner.

Instructions (Linux)

  1. From a terminal window, edit your system's bash.bashrc file. To do this with a command-line text editor like nano, execute the command sudo nano /etc/bash.bashrc (and if needed, enter your password).

  2. Use the arrow keys to find these lines:

    #if [ -f /etc/bash_completion ]; then
    #   . /etc/bash_completion
    #fi
    
  3. Un-comment each of these lines (by removing the # prefix that each line has).

  4. Save the file (you do this in nano by pushing Ctrl+o and Enter, then Ctrl+x to quit), and it should now work. Please note: for the changes to take effect in existing terminals, /etc/bash.bashrc will need to be sourced. Alternatively, logout and login again, or just reboot.

To disable it, all you need to do is re-comment each of the lines above (by adding a # to the start of each line).

Presumably, the above will also work (for your user account) if you insert the above three lines, minus their # characters, into your personal .bashrc file. If you do that, you won't need to use sudo.


According to this blog post ("Bash Completion for Mac OS X"), the instructions are different for Mac OS X. Here is what you need to do.

Instructions (Mac OS X)

  1. Make sure you have Homebrew installed, and then use it to install the package bash-completion (by typing the command brew install bash-completion).

  2. Homebrew should now tell you what you need to do to complete the installation. In this case, you need to add these three lines to your .bashrc file (using either a command-line text editor like nano which we used above, or a graphical one):

    if [ -f $(brew --prefix)/etc/bash_completion ]; then
       . $(brew --prefix)/etc/bash_completion
    fi
    
  3. You should now have auto-completion in bash. Please note: for the changes to take effect in existing shells, .bashrc will need to be sourced. Alternatively, logout and login again, or just reboot.

To disable it, all you need to do is remove the lines we added above, and run the command brew uninstall bash-completion --force.

2
  • Thanks very much. I'm actually using OSX and there is an /etc/bashrc file, but it doesn't have those lines. I've added them anyway, but there's no /etc/bash_completion file either, so I'm not sure they're doing anything...
    – flossfan
    May 2, 2014 at 13:20
  • @flossfan I just added instructions for Mac OSX. May 2, 2014 at 21:38
6

I do not have enough reputation to comment, so I am adding an answer In Ubuntu 18.04, this is what I did (to get auto completion on up and down arrows at shell)

  • edited ~/.bashrc
  • added these two lines (towards the end)
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
  • reloaded bash; (or get out of terminal and come back in)

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.