Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Suppose I have following files and directories:

% ls                                                                   
bui00293    buiawer      builds/     buiowpe/

I want to list the content of builds

% ls bui[TAB]

Zsh however shows the options with all the files and dirs above. What I want is the autocompletion of zsh's cd command ie. only autocomplete with directory names.

share|improve this question
up vote 3 down vote accepted

If you always want to complete directory names only for ls, you can put this in your .zshrc:

compdef _dirs ls

You can do fancier stuff with the “new” completion system (initialized by compinit) by playing with styles. Depending on your options, you may need to unalias ls. Then, to only ever complete directories on the ls command line:

zstyle ':completion:*:ls:*' file-patterns '*(/):directories'

You can complete only directories by default, but complete any file name if no directory matches:

zstyle ':completion:*:ls:*' file-patterns '%p:globbed-files' '*(/):directories'

You can also define a key binding to complete only directories, which you can then use anywhere.

zle -C complete_dirs .complete-word _dirs
bindkey '^X/' complete_dirs
share|improve this answer
Thanks, compdef _dirs ls solved my problem. – Lamnk Dec 25 '11 at 13:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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