Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I'm typing a filename and double-press Tab, a directory listing occurs to suggest all options for the tab completion. By default it lists in the same format as ls however I would prefer it to list the options more inline with ls -l. Is this possible?

I'm also interested to see if there's any further customization that can be done down this path, although I'm not really sure what I'd like, just some examples for ideas would be cool too.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Simply, no.

Reason:

  1. There is no option in "set" or "shopt", to specify the output format of tab completion suggestions. The only exception is COLUMNS environ, however, you can't change it to a different value.

  2. For custom completions (like --option completion), you may override the completion function with output to stdout/stderr, to display something like ls -l along with the completion suggestions. However, the filename completion is hard coded, you can't override it by the complete built-in.

Here is a short dirty example to display verbose information along with tab completion suggestions. Imagine you have a program foo and it accepts four options bar, barr, barrr, car, the dirty completion function would be:

function _foo() {
    local cmds=(bar barr barrr car)
    local cur n s
    if [ $COMP_CWORD = 1 ]; then
        cur="${COMP_WORDS[1]}"
        n="${#cur}"

        # -- dirty hack begin --
        echo
        cat <<EOT | while read s; do [ "${s:0:n}" = "$cur" ] && echo "$s"; done
bar:      choose this option if you feel well
barr:     choose this option if you feel hungry
barrr:    choose this option if you are starving
car:      choose this option if you want a car
EOT
        # ++ dirty hack end ++

        COMPREPLY=($(compgen -W "${cmds[*]}" "$cur"))
    fi
} && complete -F _foo foo

Now you can see a short helpdoc before the suggestions:

$ foo ba<tab>
bar:      choose this option if you feel well
barr:     choose this option if you feel hungry
barrr:    choose this option if you are starving
r

(The single character 'r' in the last line is auto completion to the prefix ba.)

And, when the prefix is ambiguous, the completion function is evaluated twice, the suggestions list comes at the end:

$ foo bar<tab><tab>
bar:      choose this option if you feel well
barr:     choose this option if you feel hungry
barrr:    choose this option if you are starving

bar:      choose this option if you feel well
barr:     choose this option if you feel hungry
barrr:    choose this option if you are starving

bar    barr   barrr
share|improve this answer
    
+1 A dirty hack, maybe, but I bet it could generalize nicely by making it read from a file instead of a local here-doc. –  chepner Jul 19 '12 at 16:07
add comment

Your Answer

 
discard

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.