Bash partial glob expansion

I have a question similar to this one, but different: I want bash to use a glob expansion in auto-completion, if possible. For example, I would like

$ ls *2.<TAB>

To give me:

$ ls mydoc2.
mydoc2.pdf mydoc2.tex mydoc2.txt

I face this situation quite often. In the above example I used the short prefix “mydoc,” but the actual ones are often quite long. Is this possible?

I understand *2.* would expand with C-x*, but that's a different function, which isn't suitable in this situation (because I need to choose just one file from the list of the files that match the glob).


The best method to simulate this auto-completion behavior is to enable the show-all-if-ambiguous readline variable (to display the possible completions if more than one match is found), then invoke the completion with the glob-complete-word readline command (to perform the glob pathname expansion).


# 1: Enable `show-all-if-ambiguous` in ~/.inputrc

set show-all-if-ambiguous on

show-all-if-ambiguous: This alters the default behavior of the completion functions. If set to ‘on’, words which have more than one possible completion cause the matches to be listed immediately instead of ringing the bell. The default value is ‘off’.


# 2a: Check to ensure that `glob-complete-word` is bound.

$ bind -q "glob-complete-word"

glob-complete-word can be invoked via "\eg".


# 2b: If unbound, bind `glob-complete-word` to "\eg".

$ bind '"\eg":glob-complete-word'

glob-complete-word (M-g): The word before point is treated as a pattern for pathname expansion, with an asterisk implicitly appended. This pattern is used to generate a list of matching file names for possible completions.


# 3: Trigger the autocompletion with <META-g> or <ESC-g>

# META => alt      (Windows/Linux)
# META => option   (OSX)

$ ls *2.<META-g>     

# possible completions will be listed (show-all-if-ambiguous)   
mydoc2.pdf mydoc2.tex mydoc2.txt

# glob pathname completion will be performed (glob-complete-word)
$ ls mydoc2.