How to Enable Bash Autocompletion on Mac

Bash autocompletion is a handy feature that can save you time and keystrokes when working in the terminal. It allows you to press the Tab key to automatically complete commands, file names, and more. Here, we’ll walk you through the process of enabling bash autocompletion on macOS using Homebrew.

Prerequisites

Before you begin, ensure that you have Homebrew installed. If you don’t have it installed, you can get it from Homebrew’s official website.

Installation

  1. Open your terminal.

  2. Install the bash-completion package using Homebrew by running the following command:

    1
    
    brew install bash-completion
  3. After the installation is complete, the terminal will provide you with instructions on how to enable bash autocompletion. Typically, it will ask you to add a script to your .bash_profile file.

Enabling Bash Autocompletion

To enable bash autocompletion, you need to add the following script to your .bash_profile file:

1
2
3
if [ -f /usr/local/etc/profile.d/bash_completion.sh ]; then
  . /usr/local/etc/profile.d/bash_completion.sh
fi

Here’s how you can do it:

  1. Open your .bash_profile file in a text editor. You can use a command-line text editor like nano or a graphical one like Visual Studio Code. Replace nano with your preferred text editor if needed.

    1
    
    nano ~/.bash_profile
  2. Add the following lines to the file:

    1
    2
    3
    
    if [ -f /usr/local/etc/profile.d/bash_completion.sh ]; then
      . /usr/local/etc/profile.d/bash_completion.sh
    fi
  3. Save the changes and exit the text editor.

  4. To apply the changes, either restart your terminal or run the following command:

    1
    
    source ~/.bash_profile

Testing Bash Autocompletion

To test if bash autocompletion is working, open a new terminal window or tab, and try typing a command or file path and then press the Tab key. You should see autocompletion suggestions pop up, making your terminal usage more efficient.

That’s it! You’ve successfully enabled bash autocompletion on your Mac using Homebrew. Enjoy the time-saving benefits of this feature while working in the terminal.

0%