1

I often come across bash getting in my way when I'm doing something like this:

cmake -DCMAKE_INSTALL_PREFIX=/usr/<TAB><TAB>

Apparently, bash doesn't see that cmake can accept paths and I don't get anything suggested for completion. This happens for instance on Ubuntu 14.04 with bash 4.3.42. It doesn't happen on Ubuntu 12.04 with bash 4.2.25 and on BLFS with manually-compiled bash 4.3.39.

As a workaround I have to insert ls at the beginning of the line, add a space between = and the path, and only then does bash show me the completion options. After I've written the whole path I now have not to forget to remove the added workaround.

How can I setup bash to behave without this smartness — i.e. to always suggest paths if it doesn't have any other suggestions?

1
  • Use "complete -r" in bash or ~/.bashrc. Commented Aug 12, 2023 at 7:40

1 Answer 1

3

Bash does complete file names on the command line of cmake. It's looking for a directory called -DCMAKE_INSTALL_PREFIX= containing a subdirectory called usr, but not finding anything. A quick fix to get filename completion in this scenario is to add a space after the equal sign, and remove it afterwards.

You can always complete filenames regardless of context by calling the readline function complete-filename, bound to Alt+/ by default.

I don't think bash has a generic completion fallback feature, you'd have to add that to each individual completion function. (Or switch to zsh.)

4
  • Thanks, Alt+/ is a life saver. But what do you mean by switch to zsh: what is different there regading completion?
    – Ruslan
    Commented Jun 9, 2016 at 5:41
  • 1
    @Ruslan Zsh's completion works differently and is a lot more customizable. In particular there's a generic way to set up fallbacks through zstyle, for example to try case-insensitive completion if there is no case-sensitive completion, or to try completion of a substring if there is no completion as a prefix (example). I can't write the code on the spot but it should be possible to fall back to filename completion if some other completion method fails (or what you actually wanted there, filename completion after the equal sign). Commented Jun 9, 2016 at 8:48
  • Can you provide source for readline's complete-filename (and/or the key combination it is bound to)? I can't find any reference to either in readline's manual (checked on v7.0 and even an older 6.3). Commented Aug 27, 2020 at 14:27
  • 1
    @NicApicella It's what bash calls a “readline function” (as opposed to a shell function). It's provided by bash itself, not by the readline library, so you won't find it in the readline documentation. It is a readline function in the sense that it's invoked through the readline interface, and for a bash user, it's immaterial who wrote the code to that function. Commented Aug 27, 2020 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .