bash: smart autocomplete based on history?

9

6

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?

flossfan

Posted 2014-05-02T08:48:22.200

Reputation: 91

refer to bash history search

– Kris Roofe – 2019-01-05T07:14:19.777

Answers

9

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.

joeeey

Posted 2014-05-02T08:48:22.200

Reputation: 1 396

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 – 2014-05-02T13:20:12.033

@flossfan I just added instructions for Mac OSX. – joeeey – 2014-05-02T21:38:58.677

7

Run in your bash:

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

Relogin to the ssh session, or simply reload inputrc:

bind -f  ~/.inputrc

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

Noam Manos

Posted 2014-05-02T08:48:22.200

Reputation: 771

Note: this broke by "c" key in terminals on OSX for me. – Jeremy Logan – 2019-12-02T16:46:46.930