I'm sure I've seen this before but I might have been mistaken.

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

I was wonder if there was 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

thanks

link|improve this question
Well, I know very little but the best I know of is $echo * <ENTER> You could do that before the ls * command. That's not quite hitting tab or a shortcut to expand it of course. – barlop Nov 28 '10 at 19:24
But perhaps linux users wouldn't do anything like ls * much 'cos it probably isn't necessary with ls, But also globbing behaves differently on different shells so it's not that portable, but when convenient then fine. But in ls's case, not necessary. ls */ lists directories but not so much what ls is designed to do. – barlop Nov 28 '10 at 19:27
feedback

4 Answers

up vote 3 down vote accepted

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.

link|improve this answer
It should already be bound. And Ctrl-x is already bound as a prefix to a bunch of stuff. Do bind -p | grep 'C-x' to see them. – Dennis Williamson Nov 28 '10 at 19:50
That's exactly what I was looking for! Thanks – bramp Nov 28 '10 at 19:54
@Dennis, it wasn't already bound, but adding it to my .inputrc has. I guess that's Debian's default. – bramp Nov 28 '10 at 19:55
@Dennis: It's bound to C-x* on my system. – cYrus Nov 28 '10 at 19:56
feedback

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

link|improve this answer
feedback

As a side note, under zsh the key would be Tab

link|improve this answer
feedback

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

PS: how you guys insert these nice looking keycaps with the editor ?

link|improve this answer
1  
<kbd>Esc</kbd> – cYrus Nov 29 '10 at 12:11
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.