It seems that for El Capitan simple bash improvements were completed for me:
# .bashrc function _makefile_targets { local curr_arg; local targets;
Here's how it works:
complete -F [function name] [command name] - this built-in bash register a new completion for [command name] that is generated by the bash function [function name]. Therefore, in my code above, if you type make [TAB][TAB] in your shell, you will call the _makefile_targets() function.
if [[-e "$ (pwd) / Makefile"]]; then - make sure that there is a Makefile in the current directory, otherwise do not complete the completion of bash.
grep -oE '^ [a-zA-Z0-9 _-] +:' Makefile - filter each line of the Makefile using a regular expression for the target name like "test:". -o means only returning the portion of the string that matches. For example, given a Makefile target such as "test: build package", only "test:" will be returned.
| sed 's /: //' - taking the results of grep, remove the colon from the end of the line
| tr '\ n' '' - smoosh all targets on one line, separated by one space
Inside the completion function, bash complete sets up several environment variables for you. COMP_WORDS is an array of a list of available bash choices based on what the user entered. COMP_CWORD is the index of the currently selected word.
Another very magical built-in compgen will take a list of spaces separately and filter them using the currently selected word. I do not quite understand how this works.
So, the bottom line is that the last two lines in the function filter our list of makefile targets (stored inside $targets ) and push them into the COMPREPLY array. The completion of bash reads and displays COMPREPLY as a choice in the shell.
Inspired:
source share