Autofill startup on Mac

Makefile targets are available upon completion on Linux, but AFAICS, not Mac OS (10.8.5).

Is it possible to complete work with this OS?

+6
source share
1 answer

It seems that for El Capitan simple bash improvements were completed for me:

# .bashrc function _makefile_targets { local curr_arg; local targets; # Find makefile targets available in the current directory targets='' if [[ -e "$(pwd)/Makefile" ]]; then targets=$( \ grep -oE '^[a-zA-Z0-9_-]+:' Makefile \ | sed 's/://' \ | tr '\n' ' ' \ ) fi # Filter targets based on user input to the bash completion curr_arg=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W "${targets[@]}" -- $curr_arg ) ); } complete -F _makefile_targets make 

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:

+14
source

All Articles