9

In bash is there a quick way to do tab auto-completion based on the middle of a word.

So for example, if I have these files in a directory:

001_apple.txt 002_pear.txt 003_dog.txt 

I would like to type the sequence: *d<TAB> to auto-complete 003_dog.txt.

Can this be done in bash? Is it easier to do in other shells?

5 Answers 5

6

Try ESC-g for glob expansion.

And you should always install the bash-completion package (included by default often, but you need to source it in your bash profile script).

2
  • cool trick, only issue is that it seems not allow you to cycle between multiple options Dec 30, 2008 at 1:21
  • At least if you repeat it, it lists the options.
    – PEZ
    Dec 30, 2008 at 1:27
4

Looks like zsh does this plus quite a bit more. See: expand-or-complete-prefix and COMPLETE_IN_WORD options.

Fish also does this really nicely out-of-the-box.

1
  • Both links seem to be dead. Mar 28, 2023 at 10:22
2
ls *d*<TAB>

works in bash. Not sure if that's what Ben meant. ls could of course be any other command.

4
  • Odd somehow when I type it in my shell it does not allow me to expand, ls d does work as expected but tab is not expanding it ... Dec 30, 2008 at 1:27
  • Nor in mine so, if answerer has it working, it must be a config item.
    – paxdiablo
    Dec 30, 2008 at 1:28
  • Maybe it's the "shopt -s extglob progcomp" option. Try running that command and then reattempt.
    – codelogic
    Dec 30, 2008 at 1:39
  • 1
    OP wants autocomplete, not shell globbing.
    – pihentagy
    Nov 30, 2016 at 8:56
0

You can substitute `ls *d*` to achieve the same effect, not quite as convenient as tab-completion however

1
  • Or, rather than "ls *d*", you could just use the sequence "d" which has the same effect without invoking a subshell. Or, for the truly masochistic, "$(ls $(ls $(ls d)))" :-)
    – paxdiablo
    Dec 30, 2008 at 0:42
0

I think this is a feature of readline (may even not the default keybinding):

type ls \*middle\*, then type ctrl-x, * will replace \*middle\* with the files that match the pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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