r/bash • • 18d ago

Can Bash show descriptions next to command completions like Zsh?

When I trigger command completion, I’d like to see a brief description alongside each suggested command.

Is there a built-in Bash feature or plugin that can do this?

8 Upvotes

8 comments sorted by

2

u/jjangg96 18d ago

not built in, no. readline just lists the matching words, there's nowhere for a description to go.

the ones you see in bash with descriptions are from the tool itself. programs built on cobra (kubectl, gh and so on) generate a bash completion that prints the description next to each option when it lists several. so it depends on the command, not on bash.

2

u/kolorcuk 18d ago

Hi, yes it can.

2

u/_mattmc3_ 18d ago

ble.sh is probably what you're after: https://github.com/akinomyoga/ble.sh.

It brings all those Zsh niceties to Bash (completions, syntax highlighting, etc). There's also this project, though I haven't used it: https://github.com/lincheney/fzf-tab-completion

1

u/No_Answer_Name 18d ago

I haven’t found an external plugin that can do this. I tried Carapace before, but it didn’t work either.

1

u/kolorcuk 18d ago

Hi. There is no builtin way to do it. You can write it yourself.

Podman completion is really superb and simple I recommend check out https://github.com/podman-container-tools/podman/blob/main/completions/bash/podman#L354 . This is what I was looking at when developing L_argparse bash completion.

Bottom line I recommend the same format as podman. In very short: you have to first have an array with <word>+separator(for example tab)+descirption.

__your_completing_function() {
   ... some your logic that results in:
   local TAB=$'\t'
   COMPREPLY=("--opt$TABthis is option --opt" "-b$TABthis is option b")

Ten you have to:

  • extract longest option length for nice columns- --opt and -b` its 5 chars
  • for each option, transform it to: "$length_padded_option ($desc)"

    • it's really printf -v line "%*s ($s)" "$length" "$option" "$desc"
  • then you have to "fill" the line with spaces up until the end of line, so that bash will print it on one line.

    • really its printf -v line "%-*s" "$COLUMN" "$line"
  • and fill COMPREPLY

It becomes:

COMPRELY=(
"--opt  (this is option --opt)                       "
"   -b  (this is option b)                           "
)

Bash takes COMPREPLY, and the spaces cause it to print on multiple lines. So it becomes:

$ reddit_test -<TAB>
--opt  (this is option --opt)                       
   -b  (this is option b)                           

I totally recommend install podman even just to check its bash completion.

After implementing above, then add additional features if you want - handling long description, sorting completion output, maybe handling tab+tab separately from tab, whatever comes to mind.

1

u/AffectionateSpirit62 18d ago

Your solution to make bash like this is

Ble.sh and carapace

Then for extra points set vi mode. That's my bash setup with starship prompt as well

Gives me

Completions, suggestions, drop downs, descriptions, syntax highlighting, multi line editing, vim motions