40

How can I configure the Zsh tab-completion such that when I type cd ..<TAB> it expands to cd ../ and after pressing <TAB> again proposes the folders in the parent directory for completion?

E.g. it should show the same behavior as when typing for example cd Documents<TAB> which expands to cd Documents/ and after pressing <TAB> again proposes the folders in Documents for completion.

As a starting point for configuration I use an empty .zshrc and Zsh 4.3.9 from MacPorts.

x-way
  • 876
  • 1
  • 10
  • 12

3 Answers3

76

Same problem with debian unstable, Ubuntu jaunty, both ship zsh 4.3.9. I know of multiple people with different configurations.

After reading http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=514152 I added

zstyle ':completion:*' special-dirs true

to my config and now everything works fine again.

  • 15
    `man zshcompsys` also suggests this: `zstyle -e ':completion:*' special-dirs '[[ $PREFIX = (../)#(|.|..) ]] && reply=(..)'` which avoids "./" and "../" being proposed all the time. – exic Dec 09 '11 at 16:44
  • @exic how is your code different to the answer? Your code also gives a slash each time after tab after two dots. – Timo Feb 20 '23 at 12:48
  • Just for clarity, put this in `.zshrc`, it didn't work when i added it to `.zprofile`. – TMOTTM Apr 21 '23 at 05:43
10

Put this into your ~/.zshrc:

autoload -Uz compinit
compinit

There is also a project with zsh functions/configurations for Mac OS X on http://code.google.com/p/zsh-templates-osx/ . Personally I think it's an overkill to use the whole package. But found it useful to cherry pick some tricks, functions, completions etc. and create my own .zshrc

f3lix
  • 29,500
  • 10
  • 66
  • 86
  • Thank you, this configures exactly the behavior I was looking for. – x-way Feb 19 '09 at 12:14
  • What does the option z does in your code? – Léo Léopold Hertz 준영 Apr 30 '09 at 03:44
  • autload commandline options: -U -- suppress alias expansion for functions -k -- mark function for ksh-style autoloading -t -- turn on execution tracing for functions -w -- specify that arguments refer to files compiled with zcompile -z -- mark function for zsh-style autoloading – f3lix May 03 '09 at 19:07
  • 5
    But... I have that in my .zshrc and my `.` and `..` folders don't get completed. :P What is going on there? – Victor Zamanian Feb 13 '15 at 13:04
  • 1
    I learned that the top vote answer is preferred and this answer does not always work according to @VictorZamanian , so why keep this answer? – Timo Feb 20 '23 at 14:01
-15

This may or may not be a useful answer - switch to bash, which does what you want out of the box.

  • 14
    That's somewhat like telling an vim user to switch to emacs, because he can't find a configuration switch... IMHO zsh is more powerful than bash and also comes with a great completion function. You just have to activate it... – f3lix Feb 19 '09 at 11:01