4

In bash (Ubuntu 24.10, but I guess this would apply to other versions/distributions) when you type in a console a command you can use TAB to autocomplete the filenames of the current directory. Example:

$ ls DocTAB

would propose the Document folder. This works for many commands, but not for other. On my system, for example, it does not work for sqlite3. Pressing TAB does nothing.

Why does this happen? Is it the invoked program that has to handle the keypress or (as I naively thought) it is a feature of bash or even of gnome-terminal?

10
  • It's a matter of did whoever made the package care to create a bash-autocomplete binding for it. With sqlite(3) that's a tall order, because you're allowed to name the database file anything you like. Commented Apr 4 at 8:01
  • @tink But ordinary filename completion is separate from that, right? I interpret the question as if basic filename completion (not the fancier autocompletion variant) isn't working for some commands. Commented Apr 4 at 8:03
  • Autocomplete is done by your shell (i.e. bash), typically via the bash-completion package. It might be configured in a way to call the said app, or other helpers (as any shell script could), e.g. if you're typing something like foolite3 --foo [TAB] then it might call a helper program such as foolite3 --list-available-foos to construct the offerings. The terminal does not take part in this story. Commented Apr 4 at 8:18
  • 2
    @Kusalananda - absolutely - but then I'd expect TAB expansion for files to work normally; the fact that it doesn't suggests to me that someone wrote an sqlite expansion on their distro that overrides the default. Commented Apr 4 at 17:20
  • 1
    See complete -p ls and complete -p sqlite. Commented Apr 4 at 19:11

1 Answer 1

3

If a package maintainer has included completions then the default TAB completion may have been overridden. This is the case for sqlite3 whose definitions are in: /usr/share/bash-completion/completions/sqlite3

There we see that _filedir "$dbexts" is called.

_filedir is defined in: /usr/share/bash-completions/bash_completion

Viewing its definition (on 24.04) I see that it checks if COMP_FILEDIR_FALLBACK is set. A websearch for that leads me to:

https://github.com/scop/bash-completion/blob/main/doc/configuration.md#bash_completion_filedir_fallback

BASH_COMPLETION_FILEDIR_FALLBACK

If set and not null, completions that look for filenames based on their "extensions" will fall back to suggesting all files if there are none matching the sought ones.

Available since version 2.12. Deprecated alias: COMP_FILEDIR_FALLBACK

Setting the relevant variable should make completions work closer to the way you want. (If some files match the prefix, it will still fail to find the others that don't).

Alternatively, another method mentioned in the documentation is to use a different key instead of TAB, which bypasses the filtering:

https://github.com/scop/bash-completion/blob/main/README.md#faq

Q. The bash completion code inhibits some commands from completing on files with extensions that are legitimate in my environment. Do I have to disable completion for that command in order to complete on the files that I need to?

A. No. If needed just once in a while, use M-/ to (in the words of the bash man page) attempt file name completion on the text to the left of the cursor. This will circumvent any file type restrictions put in place by the bash completion code. If needed more regularly, see the next question:

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.