Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

I'd like a tcsh'ism that I haven't been able to find: On a blank line with no content, I want to press the tab key and see the equivalent of an ls. That is to say I want

$ <tab>

to do something other then giving me a \t. I've found fantastic resources for command completion, but not for this base case. Any help on this would be great! Thanks.

share|improve this question

migrated from stackoverflow.com Jun 2 '11 at 11:52

This question came from our site for professional and enthusiast programmers.

up vote 6 down vote accepted
# expand-or-complete-or-list-files
function expand-or-complete-or-list-files() {
    if [[ $#BUFFER == 0 ]]; then
        BUFFER="ls "
        zle list-choices
        zle backward-kill-word
        zle expand-or-complete
zle -N expand-or-complete-or-list-files
# bind to tab
bindkey '^I' expand-or-complete-or-list-files
share|improve this answer
Very neat. Would it be possible to somehow hide the listing again? Tab-to-show then tab-to-hide would be nice. – Parker Coates Mar 3 '15 at 14:56
Thanks John, I found your solution and adapted here stackoverflow.com/questions/28729851/… – lolesque Mar 17 '15 at 16:25

The behavior of Tab at the beginning of a line is controlled by the insert-tab style. However, there are only two supported behaviors:

  • completion as usual, under zstyle ':completion:*' insert-tab false
  • insert a tab, under zstyle ':completion:*' insert-tab true
  • either one or the other under zstyle ':completion:*' insert-tab pending[=N]

If you just want to complete commands in that position, zstyle ':completion:*' insert-tab true will do. If you want something different, like listing the files in the current directory, you'll need to modify _main_complete.

A recent thread on the zsh-workers list discussed insert-tab.

share|improve this answer
fantastic! By _main_complete it looks like you are referencing somewhere in the C code? I'm sorry for the dumb question, but where would that be found? – user535759 May 31 '11 at 22:09
@user535759: No, _main_complete is part of the zsh code that implements completion. It's in Completion/Base/Core/_main_complete in the source tree, typically installed in a location like /usr/share/zsh/functions/Completion/Base/_main_complete. – Gilles May 31 '11 at 22:12

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.