16

If I have directory containing the files

foo.bar.a.cat
foo.bar.a.hat

and I type ls f+TAB the bash shell autocompletes to ls foo.bar.a.. If I type ls *bar*+ TAB I would like it to autocomplete to *bar*.a.. Is this possible?

This looks similar to the question

How to enable tab completion of wildcards in bash?

but the suggested answer, "it works out of the box for me", does not. I've also tried TAB TAB, with no effect. The command ``bash --versiongivesGNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)`.

3 Answers 3

19

You want either:

insert-completions

ALT* for 'insert all completions'

With this, a dir containing files name 'aa ab ac ad'
ls a* followed by alt + * would complete to ls aa ab ac ad

Man page entry on binding:

insert-completions (M-*)

Insert all completions of the text before point that would have been generated by possible-completions.

glob-expand-word

On some systems the above will not work with wildchars, the following does work on such systems for me:

ctrlx, * (a two stroke combo)

Example: I populated a dir with:

touch {a,b,c,d,e,f}{1,2,3,4,5,6,7,8,9,0}00{a,b,c,d,f,e}

I then ls *100* followed by ctrl + x, *, which turns my readline into:

ls a100a a100b a100c a100d a100e a100f b100a b100b b100c b100d b100e b100f c100a c100b c100c c100d c100e c100f d100a d100b d100c d100d d100e d100f e100a e100b e100c e100d e100e e100f f100a f100b f100c f100d f100e f100f

Man page entry for binding:

glob-expand-word (C-x *)

The word before point is treated as a pattern for pathname expansion, and the list of matching file names is inserted, replacing the word. If a numeric argument is supplied, a ‘*’ is appended before pathname expansion.

Bash Ref Man Page

8
  • 1
    This does not work (at least for me). Creating the files touch aa ab ac ad and typing ls a followed by alt+* gives the list, but doing it with ls a* does not. The wildcards are the point of the question.
    – Hooked
    May 21, 2013 at 15:38
  • @Hooked This works for me on GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) and GNU bash, version 4.2.0(1)-release (x86_64--netbsd) but not GNU bash, version 4.2.45(2)-release (i386-apple-darwin12.2.0). And by worked, I mean it filed in the text. Will check my conf files to see if I have something extra.
    – demure
    May 21, 2013 at 15:44
  • That's strange, why would such a simple thing change from version to version? Any ideas on how to enable it or is it built-in at compile time?
    – Hooked
    May 21, 2013 at 15:48
  • @Hooked I'm a bit perplexed too... It also fails on my raspberry pi GNU bash, version 4.2.37(1)-release (arm-unknown-linux-gnueabihf) (raspbian)
    – demure
    May 21, 2013 at 15:52
  • @Hooked Found alliterative binding which works on systems where the first had not, for me.
    – demure
    May 21, 2013 at 16:18
8

Try adding this to ~/.inputrc:

set show-all-if-ambiguous on

show-all-if-ambiguous makes pressing tab once (instead of twice) list all completions. It also makes the first tab press insert shared prefixes of glob expressions.

$ touch 1.0.{1,2}
$ echo *0* # I pressed tab once here
1.0.1  1.0.2
$ echo 1.0.

glob-complete-word (\eg) would also complete *0 (without * at the end) to 1.0.. It also completes for example /System/Library/Launch*/*Finde to /System/Library/LaunchAgents/com.apple.Finder.plist.

If you wanted to convert * to 1.0.1 1.0.2 in the example above, use either insert-completions (\e*) or glob-expand-word (\C-x*).

6

This problem is caused by bugs in the "bash-completion" package.

When the package is not installed bash does it's default completion, this is a pretty good generalised file name completion engine.

The "bash-completion" package is supposed to look at the rest of the line and be "smart" about the lists it generates. However, it's unable to duplicate the "glob" expansion successfully.

Personally, I think the package is trying far too hard and ends up being usable only by the "dumbest common user".

Though, before deleting it you may want to check the top of the file "/usr/share/bash-completion/bash_completion" for useful additions to your .bashrc for 'gross tuning' of the built-in bash completion.

BTW: It has over a hundred logged bugs ... https://bugs.debian.org/cgi-bin/pkgreport.cgi?package=bash-completion

1
  • 1
    $ sudo yum erase bash-completion #fixed the issue for me Nov 5, 2018 at 23:50

You must log in to answer this question.

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