bashunixcustomizationbash-completion

Customizing bash completion output: each suggestion on a new line


When you type something, you often use bash autocompletion: you start writing a command, for example, and you type TAB to get the rest of the word.

As you have probably noticed, when multiple choices match your command, bash displays them like this :

foobar@myserv:~$ admin-
admin-addrsync         admin-adduser          admin-delrsync         admin-deluser          admin-listsvn
admin-addsvn           admin-chmod            admin-delsvn           admin-listrsync

I'm looking for a solution to display each possible solution on a new line, similar to the last column on a ls -l. Ever better, it would be perfect if I could apply a rule like this: "if you find less than 10 suggestions, display them one by line, if more => actual display".


Solution

  • bash prior to version 4.2 doesn't allow any control over the output format of completions, unfortunately.

    Bash 4.2+ allows switching to 1-suggestion-per-line output globally, as explained in Grisha Levit's helpful answer, which also links to a clever workaround to achieve a per-completion-function solution.

    The following is a tricky workaround for a custom completion. Solving this problem generically, for all defined completions, would be much harder (if there were a way to invoke readline functions directly, it might be easier, but I haven't found a way to do that).

    To test the proof of concept below:

    Limitations:

    Approach:

    # Define the command (function) for which to establish custom command completion.
    # The command simply prints out all its arguments in diagnostic form.
    foo() { local a i=0; for a; do echo "\$$((i+=1))=[$a]"; done; }
    
    # Define the completion function that will generate the set of completions
    # when <tab> is pressed.
    # CAVEAT:
    #  Only works properly if <tab> is pressed at the END of the command line,
    #  i.e.,  if completion is applied to the LAST argument.
    _complete_foo() {
    
      local currToken="${COMP_WORDS[COMP_CWORD]}" matches matchCount
    
      # Collect matches, providing the current command-line token as input.
      IFS=$'\n' read -d '' -ra matches <<<"$(compgen -A file "$currToken")"
    
      # Count matches.
      matchCount=${#matches[@]}
    
      # Output in custom format, depending on the number of matches.
      if (( matchCount > 1 && matchCount < 10 )); then
    
          # Output matches in CUSTOM format:
          # print the matches line by line, directly to the terminal.
        printf '\n%s' "${matches[@]}" >/dev/tty
          # !! We actually *must* pass out the current token as the result,
          # !! as it will otherwise be *removed* from the redrawn line,
          # !! even though $COMP_LINE *includes* that token.
          # !! Also, by passing out a nonempty result, we avoid the bell
          # !! signal that normally indicates a failed completion.
          # !! However, by passing out a single result, a *space* will
          # !! be appended to the last token - unless the compspec
          # !! (mapping established via `complete`) was defined with 
          # !! `-o nospace`.
        COMPREPLY=( "$currToken" )
          # Finally, simulate redrawing the command line.
            # Obtain an *expanded version* of `$PS1` using a trick
            # inspired by https://stackoverflow.com/a/24006864/45375.
            # !! This is NOT foolproof, but hopefully works in most cases.
        expandedPrompt=$(PS1="$PS1" debian_chroot="$debian_chroot" "$BASH" --norc -i </dev/null 2>&1 | sed -n '${s/^\(.*\)exit$/\1/p;}')
        printf '\n%s%s' "$expandedPrompt" "$COMP_LINE" >/dev/tty
    
    
      else # Just 1 match or 10 or more matches?
    
          # Perform NORMAL completion: let bash handle it by 
          # reporting matches via array variable `$COMPREPLY`.
        COMPREPLY=( "${matches[@]}" )    
    
      fi 
    
    }
    
    # Map the completion function (`_complete_foo`) to the command (`foo`).
    # `-o nospace` ensures that no space is appended after a completion,
    # which is needed for our workaround.
    complete -o nospace -F _complete_foo -- foo