tags:

views:

339

answers:

5

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?

A: 

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

Ben
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
+1  A: 

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).

PEZ
cool trick, only issue is that it seems not allow you to cycle between multiple options
Sam Saffron
At least if you repeat it, it lists the options.
PEZ
A: 

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.

Sam Saffron
+2  A: 
ls *d*<TAB>

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

codelogic
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 ...
Sam Saffron
Nor in mine so, if answerer has it working, it must be a config item.
paxdiablo
Maybe it's the "shopt -s extglob progcomp" option. Try running that command and then reattempt.
codelogic
This works in my bash shell.
nicerobot
A: 

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.