Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Some of the git commands have many options, and it would often be useful to search through them for the one I need - I was just looking for the option which controls the TAB width in git-gui, but there are about 200 completions for git config. An obvious workaround is to copy all the completions into an editor and search through them, but I'd rather do

[something] | grep tab

There are no man or info pages for compgen, help compgen doesn't even explain its own options, and there's no auto-complete for compgen (how's that for irony?).

PS: compgen -A doesn't work.

PPS: This is not a question about git-gui - The solution to the tab width question was elsewhere.

PPPS: This is not about auto-completing commands, only command parameters.

share|improve this question
This is very similar to How to pipe the output of "tab complete"?, but the accepted answer there was specifically for completing commands, not arguments – Michael Mrozek Dec 2 '11 at 16:11

1 Answer 1

up vote 7 down vote accepted

You can use the following function, which use the same way sudo auto-completion generate the completion list:

comp() {
    local COMP_LINE="$*"
    local COMP_WORDS=("$@")
    local COMP_CWORD=${#COMP_WORDS[@]}
    local COMP_POINT=${#COMP_LINE}
    local COMP_WORDBREAKS='"'"'><=;|&(:"
    # Don't really thing any real autocompletion script will rely on
    # the following 2 vars, but on principle they could ~~~  LOL.
    local COMP_TYPE=9
    local COMP_KEY=9
    _command_offset 0
    echo ${COMPREPLY[@]}
comp git config ''

where _command_offset is defined in bash-completion (package).

NOTE: the function need to be run in a interactive shell (i.e. if it is in a file, the file need to be sourced instead of just run.) or the necessary completion rules/functions will not be defined.

PS. compgen -A only works for builtin actions, what you should have tried (but doesn't work either) is compgen -F (or actually compgen -o bashdefault -o default -o nospace -F _git). The reason this doesn't work (and doc for bash built-in commands including compgen/complete) can be found in bash(1).

share|improve this answer
@l0b0 hopefully works in all cases. – yuyichao Feb 7 '12 at 14:37
Excellent, it works! I did a couple minor tweaks and now it's in my .bash_aliases for good. You might want to prune so only the solution is left, but there's no doubt you get the bounty. I'll remove my discussion comments since they are not relevant anymore. – l0b0 Feb 7 '12 at 16:26
I have found that using this in the interactive shell can destroy the tab-completion already in place. Making each of the variables local to the function solves that problem. (e.g. local COMP_LINE="$*") – A. L. Flanagan Feb 16 at 16:09
@A.L.Flanagan Good point. Updated. – yuyichao Feb 17 at 4:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.