Bash completion is a useful functionality that allows you to auto-complete commands, filenames, and arguments within a bash shell. This saves time, increases efficiency, and enhances the user experience when working in a terminal environment.
The latest version of macOS uses Zsh as its default shell, having switched from Bash with the Catalina release in 2019. However, if you still prefer to use Bash and wish to enable bash completion, here’s a comprehensive guide to help you.
Step 1: Install Homebrew
Homebrew is a popular package manager for macOS that simplifies the installation of software on Apple’s macOS operating system and Linux. If you haven’t already installed Homebrew, you’ll need to do so to proceed.
To install Homebrew, open a terminal and execute the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Step 2: Install Bash-Completion
Once Homebrew is installed on your macOS, you can use it to install bash-completion. This can be done by executing the following command:
brew install bash-completion
Please note that if you’re using Bash 3.2 (which is the default on macOS), you should use bash-completion. However, if you have upgraded to Bash 4.1 or newer, you should use bash-completion@2 instead:
brew install bash-completion@2
Step 3: Enable Bash Completion
The above step only installs bash completion. The next step is to enable it by adding it to your .bash_profile or .bashrc file.
- If you’re using bash-completion, add the following lines to your .bash_profile or .bashrc:
if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi
- If you’re using bash-completion@2, add these lines:
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d" [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
You can use a text editor like nano or vi to add these lines. For example, to add them using nano, you would type:
nano ~/.bash_profile
Then you can paste the lines at the end of the file, save the changes and close the editor (with nano, you can do this by pressing CTRL+O to save and CTRL+X to exit).
Step 4: Source your .bash_profile or .bashrc
For the changes to take effect, you’ll need to source your .bash_profile or .bashrc:
source ~/.bash_profile
or
source ~/.bashrc
If you’re unsure which of these files you’re using, try sourcing both of them. Only one is typically used, but it won’t hurt to source both.
Step 5: Verify Bash Completion
To verify that bash completion has been successfully installed and enabled, you can try typing part of a command, followed by the Tab key. If it auto-completes, then you know it’s working.
For instance, type brew followed by a space and then hit Tab twice. If you see a list of possible commands, bash completion is enabled.
And that’s it! You’ve now successfully enabled bash completion on macOS. This should greatly improve your efficiency when working in a bash environment, reducing the need to remember and type out long commands.
1 Comment
Hi according to > brew info bash-completion@2
The right way to enable it is:
Add the following line to your ~/.bash_profile:
[[ -r “/opt/homebrew/etc/profile.d/bash_completion.sh” ]] && . “/opt/homebrew/etc/profile.d/bash_completion.sh”