I'm trying to write a small command launcher application, and would like to use bash's tab completions in my own completion system. I've been able to get a list of completions for general commands using compgen -abck
.
However, I would also like to get completions for specific commands: for instance, the input git p
should display completion for git's commands.
Is there any way I can use compgen
to do this? If not, are there any other ways I can get a list of completions programmatically?
[EDIT: To clarify, I'm not trying to provide completion to bash - my app is a GUI command launcher. I'd simply like to use bash's existing completions in my own app.]
Bourne shell and csh do not, but ksh, bash, tcsh, and zsh all have tab completion to varying degrees. The basic principle in all of these shells is the same; you type the start of the word, hit the <TAB> key twice, and the list of possible commands or files is displayed.
A. 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.
Depending on your package manager, you have to manually source this file in your ~/. bashrc file. Reload your shell and verify that bash-completion is correctly installed by typing type _init_completion .
I don't really know how it works, but the awesome window manager uses the following Lua code for getting access to bash completion's result:
https://github.com/awesomeWM/awesome/blob/master/lib/awful/completion.lua#L119
complete -p
we find complete -o bashdefault -o default -o nospace -F _git git
. We remember "_git" for later.All together we use the following script:
__print_completions() { printf '%s\n' "${COMPREPLY[@]}" } # load bash-completion functions source /etc/bash_completion # load git's completion function _completion_loader git COMP_WORDS=(git l) COMP_LINE='git l' COMP_POINT=6 COMP_CWORD=1 _git __print_completions
Output: "log"
Check in the /etc/bash_completion.d/
directory. This is where the different command completion scripts stay.
Quite an old question, but in the mean time I've implemented a script that handles this to reuse completions with ZSH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With