Makefile completion on steroids!
I had 2 problems with the normal completions:
Problem #1
Sometimes you have targets you want to call like make greet:hi
and make greet:hola
sort of like namespacing Makefile
target names. So your Makefile
ends up looking like:
greet:hola:
echo "hola world"
# OR a .PHONY target
.PHONY: greet:hi
greet:hi:
echo "hi world"
In this case the auto-completions after :
don t show up as it uses :
in the Makefile as shown above.
Problem #2
There wasn t a way to navigate through the list of all Makefile
targets that match my input using arrow keys (or CTRL-p
/ CTRL-n
) in my bash
shell.
Basically, I wanted to use fuzzy search like approach on the targets (i.e. fzf
).
FZF Repo: https://github.com/junegunn/fzf
Solution
Install FZF Dependency
Using Homebrew
You can use Homebrew (on macOS or Linux)
to install fzf.
brew install fzf
$(brew --prefix)/opt/fzf/install
Using Linux package managers
Package Manager |
Linux Distribution |
Command |
APK |
Alpine Linux |
sudo apk add fzf |
APT |
Debian 9+/Ubuntu 19.10+ |
sudo apt-get install fzf |
Conda |
|
conda install -c conda-forge fzf |
DNF |
Fedora |
sudo dnf install fzf |
Nix |
NixOS, etc. |
nix-env -iA nixpkgs.fzf |
Pacman |
Arch Linux |
sudo pacman -S fzf |
pkg |
FreeBSD |
pkg install fzf |
pkgin |
NetBSD |
pkgin install fzf |
pkg_add |
OpenBSD |
pkg_add fzf |
XBPS |
Void Linux |
sudo xbps-install -S fzf |
Zypper |
openSUSE |
sudo zypper install fzf |
FZF and :
compatible auto-complete command
Put this in your .bashrc
complete -W "`grep -oE ^[a-zA-Z0-9_.-]+[\:]*[a-zA-Z0-9_.-]+:([^=]|$) ?akefile | sort | uniq | sed s/[^a-zA-Z0-9_.-]*$// | sed s/[]//g | fzf`" make
Now just typing make
and then hitting the key will work!
DEMO: in action!
Then you can use as following:
make using fzf