41

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.

3
  • 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, 2010 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, 2010 at 19:27
  • Yeah but @barlop I'm not sure any command has a use case for just expanding * and then running it - but the use case for all of them is when you want to expand them all and then edit a few random ones out that are too hard to hit with a regex before you hit enter.
    – NeilG
    Sep 14, 2023 at 0:24

5 Answers 5

25

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.

4
  • 9
    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. Nov 28, 2010 at 19:50
  • That's exactly what I was looking for! Thanks
    – bramp
    Nov 28, 2010 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, 2010 at 19:55
  • 3
    @Dennis: It's bound to C-x* on my system.
    – cYrus
    Nov 28, 2010 at 19:56
28

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

15

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

3
  • Just to be clear to vimmers, first type the asterisk while typing the command line normally, then escape out of edit mode, cursor will locate on the asterisk if you escape immediately, then hit asterisk with the cursor located on the asterisk and the glob will expand and drop you in edit mode again.
    – NeilG
    May 23, 2019 at 9:50
  • @NeilG There is actually no need type this first asterisk. ls <Esc>* would work equally fine.
    – jlliagre
    Sep 13, 2023 at 13:13
  • 1
    Ah, thanks for that, @jlliagre. I did some playing and found out there are a few pre-conditions for expansion but having an * on the command line is not one of them. The command line has to be somehow ready for typing arguments, so for instance, if you're still typing the command itself and have no preceding space it won't expand. Either way, very convenient and intuitive access to this feature without having to use chords or remember which letter to press if you're in vi mode.
    – NeilG
    Sep 14, 2023 at 0:41
6
$ 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

2
  • This is the best answer because it looks up the exact keybinding, which could be different in different environments.
    – wisbucky
    May 4, 2018 at 1:07
  • No it's not the best answer, @wisbucky, in my case I get glob-expand-word is not bound to any keys. What then? The accepted answer explains how to bind to what you want, or the conventional in fact, which is ctrl-x. This is a less useful answer added 7 years after the accepted answer and except for mentioning bind which is also mentioned in comments to the accepted answer (also written 7 years before this answer) it would otherwise be spam.
    – NeilG
    Sep 14, 2023 at 0:34
0

An alternative to glob-expand-word (\C-x*) is insert-completions (\e*). It works without an asterisk at the end, but it also includes other completions like hidden files. I have rebound both in ~/.inputrc:

# insert glob results (\C-x* by default)
"\C-g": glob-expand-word

# insert completion list (\e* by default)
"\ei": insert-completions

glob-complete-word (\eg) can be used to convert for example /System/Library/Launch*/*Finder to /System/Library/LaunchAgents/com.apple.Finder.plist.

You must log in to answer this question.

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