3

I have a folder with subfolders named like this:

1122334 important things
1122335 less important things
1122336 notes
1122337 pictures of kittens

etc

The numbers at the beginning are date codes. If it was up to me, I would put the date codes at the end, but it isn't.

In bash, I'd like to do something like this:

$ cd *pictu<tab>

and get this

$ cd 1122337\ pictures\ of\ kittens/

Is there a simple way to do this, or something I can put in my .bashrc to make this possible?

  • I just found out that zsh can do this. Another option would be to cd *pic* which will move you to the directory but not tab expand its name. – terdon Nov 13 '13 at 17:54
  • Awesome! It looks like bash will do the same thing. I wonder if there is a way to get autocomplete to work with it. – OhmArchitect Nov 13 '13 at 18:00
  • I meant that bash will allow you to cd into a directory with cd *pic* if that expands to a single directory, zsh can also tab expand the name if you start typing pic<TAB> which bash won't. If that is all you needed should I post it as an answer? – terdon Nov 13 '13 at 18:04
  • Ah! Sorry I misread that. In bash if I do cd *pic*<TAB> it will autocomplete for me. Yes, post this as an answer! (Thanks!!) – OhmArchitect Nov 13 '13 at 18:13
4

As far as I know, there is no way of making bash autocomplete *pictu, but here are some workarounds:

  1. Don't use TAB, just cd directly using wildcards before and after the pattern:

    $ cd *pictu*
    

    That will move you into the first directory whose name contains pictu.

  2. Use two wildcards and then TAB:

    $ cd *pictu*<TAB>
    

    That should expand to cd 1122337\ pictures\ of\ kittens/

  3. Use another shell. zsh has a cool feature, you can do:

    ➜ cd pictu<tab>
    

    and that expands to ➜ cd 1122337\ pictures\ of\ kittens/.

  • Note that in order for $ cd *pictu*<TAB> to expand in bash, the bashdefault completion option has to be set for cd: compopt -o bashdefault cd. You can review the currently set completion options with the command complete -p cd. – Thomas Nyman Nov 14 '13 at 5:58
  • @ThomasNyman could you elaborate on that please? On my system, complete -p cd shows that I have nospace on and nothing else but I can auto complete cd *bar* to cd fobarfoo. – terdon Nov 14 '13 at 12:45
  • I tested this on Ubuntu 12.04 with bash 4.2.25 and for me the two-wildcard-expansion does not work without adding the bashdefault completion option. I have nospace set also, and on Ubuntu, installing the bash-completion package adds a _cd completion function as well, but these do not seem to affect the behaviour in my case. – Thomas Nyman Nov 15 '13 at 7:44
  • @ThomasNyman I'm on bash 4.2.45, complete -p cd returns complete -o nospace -F _cd cd but the two-wildcard-expansion works. It might be because of the newer bash I guess. – terdon Nov 15 '13 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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