4

I remember reading a trick in a stackoverflow comment somewhere for getting a list of filenames in the terminal, but I can't exactly remember it.

If you have a bunch of files like:

foo-a.txt
foo-b.txt
foo-c.txt

There is a trick that goes something along the lines of:

  1. type cat foo-*
  2. Press something like tab, backspace, esc (this does not work, but is the step I want to know)
  3. The command line should now have cat foo-a foo-b foo-c

Note that is is different to the basic tab completion which shows a list of filenames below the command. This will put all the filenames in the command input so that pressing "enter" will execute cat with all the files.

Can anyone enlighten me?

2 Answers 2

5
  1. Type cat foo-*
  2. Press Ctrl+X then * (asterisk/star) to expand the glob
    • If that doesn't work, run bind -p | grep glob-expand-word and check if it says "\C-x*": glob-expand-word. If not, you can set it:
      bind '"\C-x*": glob-expand-word'
      
  3. The command line should now be cat foo-a.txt foo-b.txt foo-c.txt

glob-expand-word vs insert-completions

OP's answer is about insert-completions (Esc, * or Alt+*), which is similar but different to glob-expand-word:

  • glob-expand-word expands filename globs.
  • insert-completions inserts all the possible completions that pressing Tab would show.

So that means you can also use insert-completions for things besides filenames, like options. For example type ls --f, press Esc, *, and you will get ls --file-type --format=, though I'm not sure how useful that is.

And that means that it behaves differently when expanding globs. For example if you type cat foo-* and press Esc, *, it will expand to only the first completion: cat foo-a.txt.

2
  • Thanks, that it! Btw: You can also use esc instead of ctr+x
    – mdsimmo
    Feb 3, 2019 at 8:59
  • 1
    @mdsimmo Esc, * is a different function actually. I just checked bind -p | grep '\\e\*' and it says "\e*": insert-completions. I'm not clear on the exact difference, but if you type cat * then press Esc, *, it will expand to just the first matching item.
    – wjandrea
    Feb 3, 2019 at 9:02
2

I found the original comment here. (Can someone with more points upvote him for me? :P)

The sequence is:

  • Type cat foo-
  • Press esc
  • Press * (asterisk)

The same sequence seems to work with any program (not just cat)

2
  • 1
    Upvoted the comment per request :p (BTW, worth noting that comment upvotes don't count towards reputation points)
    – wjandrea
    Feb 3, 2019 at 22:16
  • 1
    Alt+* also works (Alt+Shift+8 on a US keyboard). And in general any terminal shortcut that uses Esc, key can also be typed as Alt + key.
    – wjandrea
    May 29, 2019 at 20:50

You must log in to answer this question.

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