Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash completion of makefile target

Suppose I have a simple makefile like:

hello:    echo "hello world"  bye:    echo "bye bye" 

Then in bash I want something like:

make h < tab >

so it can complete to

make hello

I found a simple way like creating empty files hello and bye but I'm looking for something more sophisticated.

like image 633
Guillaume Massé Avatar asked Nov 15 '10 20:11

Guillaume Massé


People also ask

How can I tell if I have bash completion?

You can use the complete command with the -p option to get a list of all or specific completions.

What does bash completion do?

Bash completion is a functionality through which bash helps users type their commands faster and easier. It accomplishes that by presenting possible options when users press the tab key while typing a command.

Where are bash completions stored?

These completions are loaded only on demand. Completions stored in file ~/. bash_completion are loaded always.


2 Answers

Add this in your ~/.bash_profile file or ~/.bashrc file

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

This searches for a target in your Makefile titled 'Makefile' or 'makefile' (note the capital ? wildcard in ?akefile) using grep, and pipes it over to the complete command in bash which is used to specify how arguments are autocompleted. The -W flag denotes that the input to the complete command will be a wordlist which is accomplished by passing the results of grep through sed which arranges it into the desirable wordlist format.

Caveats and gotchas:

  1. Your make file is named 'GNUMakefile' or anything else other than 'Makefile' or 'makefile'. If you frequently encounter such titles consider changing the regular expression ?akefile accordingly.

  2. Forgetting to source your ~/.bash_profile or ~/.bashrc file after making the changes. I add this seemingly trivial detail since, to the uninitiated it is unfamiliar. For any change to your bash files to take effect, source them using the command

    source ~/.bashrc 

    or

    source ~/.bash_profile 

PS. You also now have the added ability to display the possible make targets by pressing [Tab] twice just like in bash completion. Just make sure you add a space after the command make before typing [Tab] twice.

like image 113
Cibin Joseph Avatar answered Sep 21 '22 16:09

Cibin Joseph


Could this be what you're looking for?

http://freshmeat.net/projects/bashcompletion/

make [Tab] would complete on all targets in Makefile. This project was conceived to produce programmable completion routines for the most common Linux/UNIX commands, reducing the amount of typing sysadmins and programmers need to do on a daily basis.

like image 33
Kos Avatar answered Sep 23 '22 16:09

Kos