Bash – Is it possible for bash/readline to “clean up” completion suggestions out the terminal

autocompletebashreadline

If you are using zsh and do a tab completion for commands, pathnames, options etc, whenever there are multiple potential matches the suggestions will be displayed below the prompt.
Once you chose a suggestion zsh will remove the list of suggestions out of the terminal, as you can see here:

enter image description here

Image source

In contrast when bash offers completion suggestions it outputs the list and returns you to a new prompt.

enter image description here

Image source
The zsh behaviour is preferable to me as the suggestions I never used offer no value and just produce more "noise" in the terminal.
Is it possible to configure bash/readline to behave like zsh in this way?

Best Answer

short: it's possible but complex as .inputrc bindings.

long: As suggested in a comment, you could do something like this in your bash prompt and readline bindings.

Saving/restoring the cursor would be ineffective, since the only point at which you would be able to reliably clear the remainder of the screen would be on pressing Enter to complete the selection.

Once you've passed control to accept-line, it is too late to clear the remainder of the screen. It's possible (but complex) to define a series of real and ad hoc "key bindings" to make readline to do more than one operation. See for example

But that approach limits you to sending characters to bash and issuing commands to readline. None of the readline commands does

printf '\033[J'

to clear the remainder of the screen. The closest would be readline's built-in clear-screen (not what you want). Your binding would have to do something like

  • beginning-of-line
  • insert "printf '\033[J';"
  • end-of-line
  • accept-line

The end-of-line would work around a quirk of readline. It allows you to press Enter anywhere on the line. If your cursor were in the middle of the line, you'd have just a fragment of your input left visible (although bash would get the entire string).