How to `cd` with word in the middle of a folder?

I can't speak for others (e.g., zsh) but if you are using bash, wildcards do work to an extent.  Example:

~ $ ls
Documents
Desktop
Downloads

If you use an asterisk (*), you get:

~ $ cd *ments
~/Documents $

That's because bash can do the substitutions before the command ever gets to cd.

In the case of cd, if multiple matches work, you would expect the behaviour to be undefined:

~ $ cd *s
bash: cd: too many arguments

bash expands this to cd Documents Downloads, which doesn't make sense to cd.


You can also rely on bash's autocomplete.  In your example, you can simply type cd t; then hitting Tab will auto-complete to: cd this.is. or whatever the next ambiguous character is.  Hit Tab a second time to see all options in this filtered set.

You can repeat by entering another character to narrow it down, Tab to autocomplete to the next ambiguous character, and then Tab to see all options.


Going further, bash can handle wildcards in autocomplete. In the first case above, you can type cd D*s then hit Tab to get suggestions of what could match the pattern:

~ $ cd D*s
Documents/ Downloads/
~ $ cd D*s

If only one match exists, it'll get completed for you.

~ $ cd *loads
~ $ cd Downloads/

You could also use ls if you don't mind being in the questioned directory. The -d tells ls to list directories themselves instead of their contents.

$ ls -d *long*
this.is.very.long.name.context
this.is.another.long.path.authors

or you could use find if you want to look recursively:

$ find workspace -type d -name '*long*'
workspace/this.is.very.long.name.context
workspace/this.is.another.long.path.authors

With zsh, you can configure the completion system with zstyles so it complete words in the middle.

The compinstall function can assist you with that. Run it as autoload compinstall; compinstall for instance. It's also available through the zsh-newuser-install menu often invoked upon first use of zsh (when you don't have a .zshrc yet).

               *** compinstall: main menu ***
2.  Matching control: set behaviour for case-insensitive matching,
    extended (partial-word) matching and substring matching.
              *** compinstall: matcher menu ***

`Matchers' compare the completion code with the possible matches in some
special way.  Numbers in parentheses show matchers to be tried and the order.
The same number can be assigned to different matchers, meaning apply at the
same time.  Omit a sequence number to try normal matching at that point.
A `+' in the first line indicates the element is added to preceding matchers
instead of replacing them; toggle this with `t'.  You don't need to set
all four, or indeed any matchers --- then the style will not be set.

   (    )   `+' indicates add to previous matchers, else replace
n. (    ) No matchers; you may want to try this as the first choice.
c. (    ) Case-insensitive completion (lowercase matches uppercase)
C. (    ) Case-insensitive completion (lower/uppercase match each other)
p. (    ) Partial-word completion:  expand 'f.b' to 'foo.bar', etc., in one go.
          You can choose the separators (here `.') used each time.
s. (    ) Substring completion:  complete on substrings, not just initial
          strings.  Warning: it is recommended this not be used for element 1.

If you make it just:

   (    )   `+' indicates add to previous matchers, else replace
n. (1   ) No matchers; you may want to try this as the first choice.
c. (    ) Case-insensitive completion (lowercase matches uppercase)
C. (    ) Case-insensitive completion (lower/uppercase match each other)
p. (    ) Partial-word completion:  expand 'f.b' to 'foo.bar', etc., in one go.
          You can choose the separators (here `.') used each time.
s. ( 2  ) Substring completion:  complete on substrings, not just initial
          strings.  Warning: it is recommended this not be used for element 1.

and save and exit, you'll see compinstall added this line to your ~/.zshrc:

zstyle ':completion:*' matcher-list '' '' '' 'l:|=* r:|=*'

info zsh matcher-list (note: you may need to install a zsh-doc package) will tell you how that works.

Then if you type cd anoTab, you'll see it being completed to this.is.another.long.path.authors assuming there's no file whose name starts with ano.

I invite you to go through all the compinstall menus as there's a lot more amazing features in there you can enable. (same for zsh-newuser-install).