How to expand * on Bash command line

I understand that if you type ls * it is actually expanded to ls a b c when the current directly has files a, b and c.

I was wondering if there is a way to expand this before I hit enter. Similar to how Ctrl+X works, or tab complete works.

So to make myself clear

$ ls *
<press magic key>
$ ls a b c

in a similar way to:

$ ls ~/
<press tab>
$ ls /home/username

I thought I'd seen this before but I might have been mistaken.


You can use the glob-expand-word function, from man bash:

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, an
asterisk is appended before pathname expansion.

Add something like this to your ~/.inputrc:

Control-x: glob-expand-word

So $ ls * followed by Ctrl-X will expand to $ ls a b c, in your example.


In bash, the readline capability is called glob-expand-word and is bound to CtrlX* by default.


When you are in vi mode (set -o vi), the "magic key" is Esc*. This works with both bash and ksh.


$ bind -q glob-expand-word
glob-expand-word can be invoked via "\C-x*".

$ bind -q insert-completions
insert-completions can be invoked via "\e*".

So to use these we can do

ls * Ctrl+x *

or

ls * Esc *

Expand complicated lines before hitting enter