Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bundle bash completion with a program and have it work in the current shell?

I sweated over the question above. The answer I'm going to supply took me a while to piece together, but it still seems hopelessly primitive and hacky compared to what one could do were completion to be redesigned to be less staticky. I'm almost afraid to ask if there's some good reason that completion logic seems to be completely divorced from the program it's completing for.

like image 947
psp Avatar asked Apr 11 '10 07:04

psp


People also ask

Does bash have tab completion?

Bash completion is a functionality through which Bash helps users type their commands more quickly and easily. It does this by presenting possible options when users press the Tab key while typing a command.

Where do bash completion files go?

Put them in the completions subdir of $BASH_COMPLETION_USER_DIR (defaults to $XDG_DATA_HOME/bash-completion or ~/. local/share/bash-completion if $XDG_DATA_HOME is not set) to have them loaded automatically on demand when the respective command is being completed.

How do I know if bash completion is installed?

If the autocomplete results contain directories only (no files), then Bash Completion is installed. If the autocomplete results include files, then Bash Completion is not installed.


1 Answers

I wrote a command line library (can be seen in scala trunk) which lets you flip a switch to have a "--bash" option. If you run

./program --bash

It calculates the completion file, writes it out to a tempfile, and echoes

. /path/to/temp/file

to the console. The result is that you can use backticks like so:

`./program --bash`

and you will have completion for "program" in the current shell since it will source the tempfile.

For a concrete example: check out scala trunk and run test/partest.

like image 143
psp Avatar answered Oct 17 '22 21:10

psp