Bash auto-completion for Makefile targets in macOS

How to get Makefile target auto-completion working in macOS bash?

First make sure you have bash-completion installed.

Then add this to your .bashrc file:

complete -W "\`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' Makefile | sed 's/[^a-zA-Z0-9_.-]*$//'\`" make

This assumes your Makefiles are all names Makefile (with capital M) - if not you will need to adapt this command to your specific file naming scheme.

If you want to exclude the dot-targets from suggestions (including all .PHONY special targets) you can use its slightly modified version:

complete -W "\`grep -oE '^[^.][a-zA-Z0-9_.-]+:([^=]|$)' Makefile | sed 's/[^a-zA-Z0-9_.-]*$//'\`" make