Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I use standard tab completion in bash or zsh, I type the first part of a filename or path, then hit to complete the word. This works well unless I'm in a directory full of files where all of the variation in filenames is at the end of the word, e.g. A directory full of time-stamped files which all start with the same prefix:

foo-20120701124501.log
foo-20120701124531.log
foo-20120701124601.log
foo-20120701124631.log
foo-20120701124701.log
foo-20120701124731.log

In this case, if I type foo<tab> I'll get foo-20120701124 -- I can tab again, and see all of the files... I would have to type in the next 3 characters to get to a unique file name.

I'm wondering if there is a way to type for instance 4731.log and then complete beginning of the word. I'm guessing that this would be difficult in bash, because bash completion uses $COMP_CWORD and $COMP_KEY for its programmable completion, and from the way that I'm reading it, I don't think that those would allow for leftward word completion. I don't know enough about completion inside zsh to know where to start looking in the man pages.

share|improve this question
up vote 2 down vote accepted

This will only work if there is a leading command before hitting tab. Likely is not what you are looking for but the only was I could get the desired results. The below example should work in your case. You just need to be at the end of the *4731.log and hit tab 1 time. If it is unique it will autocomplete.

ls *4731.log
share|improve this answer
    
Yep, worked like a charm. I generated a series of files using for i in {1..10}; do touch $(date -d "$i days $RANDOM seconds ago" +%Y%m%d%H%M%S); done... this created a series of files which looked like 20120915165427 20120916121328 20120917180952 etc. If I use ls *09<tab>, it doesn't expand, but *09*<tab> does. – Barton Chittenden Sep 15 '12 at 1:05

You must log in to answer this question.

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