Take the 2-minute tour ×
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.

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
add comment

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

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

2 Answers

up vote 4 down vote accepted
# expand-or-complete-or-list-files
function expand-or-complete-or-list-files() {
    if [[ $#BUFFER == 0 ]]; then
        BUFFER="ls "
        CURSOR=3
        zle list-choices
        zle backward-kill-word
    else
        zle expand-or-complete
    fi
}
zle -N expand-or-complete-or-list-files
# bind to tab
bindkey '^I' expand-or-complete-or-list-files
share|improve this answer
add comment

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
1  
@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
add comment

Your Answer

 
discard

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.