This answer is far from complete. To grep all the variables in the Makefile, we use make -p to print the Makefile database:
# GNU Make 3.81
We look for lines starting with # makefile (from 'Makefile', line xy) and extract the name of the following variable:
$ make -p | sed -n '/# makefile (from/ {n; p;}' MAKEFILE_LIST := Makefile DIR :=
In the next step, we will delete everything except the variable name (everything after := ):
$ make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1/p;}' MAKEFILE_LIST DIR
The following lines show how to do this:
_make_variables() { # get current completion local cur=${COMP_WORDS[COMP_CWORD]} # get list of possible makefile variables local var=$(make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1=/p;}') # don't add a space after completion compopt -o nospace # find possible matches COMPREPLY=( $(compgen -W "${var}" -- ${cur}) ) } # use _make_variables to complete make arguments complete -F _make_variables make
Now make D[tab] leads to make DIR= .
Unfortunately, you will lose the entire file and achieve the goal with this approach. It would also be useful to remove a few more variables (e.g. MAKEFILE_LIST ) from the completion output.
It might be worth adding a wish / bug report to the bash -completion project to add this feature.
sgibb source share