1

Customizing zsh allows you to simply hit the tab key and let you cycle through directories. See this answer.

That is an amazing workflow improvement, but I need help with the following:

How can I achieve, that zsh tab completion will show me ALL files and folders and lets me cycle through them? (Actually it only shows files when there is no more directory to change to.)

In addition, it would be very useful, that it will not put "cd" in front of the completion when the choice is a file and not a folder.

(I use the systems mime to open files from terminal.)

Thanks.

Community
  • 1
  • 1
  • If you use the "script" given in the answer from the other user, then hitting the tab key will allow you to "tab through" directories (AND only to directories at the current status...). I also want, that i am possible to tab through files and of course execute them. First problem is, you can only tab through files if there are no more directories to tab through, and second it puts "cd" at the beginning of the line then... even if its a file... – stackunderflow Jun 07 '15 at 23:28
  • 2
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306). – jww Jun 08 '15 at 00:15
  • 1
    @jww I failed to see why it is not. Programming ZLE widgets is a kind of programming. – 4ae1e1 Jun 08 '15 at 00:16

1 Answers1

2

Modifying the answer here slightly:

function complete_pwd_items_on_empty_buffer
{
    if [[ -z $BUFFER ]]; then
        BUFFER="./"
        CURSOR=2
        zle list-choices
    else
        zle expand-or-complete
    fi
}
zle -N complete_pwd_items_on_empty_buffer
bindkey '^I' complete_pwd_items_on_empty_buffer

This will insert ./ and list executable files or directories if the command line is empty and you press the TAB key. You can execute an executable file in the current directory tree this way, or cd into a subdirectory this way if you have set the AUTO_CD option.

In fact we can do a little bit better than that by enabling this trick on a command line with whitespace only:

function complete_pwd_items_on_empty_buffer
{
    if [[ $BUFFER =~ ^[[:space:]]*$ ]]; then
        BUFFER+="./"
        CURSOR+=2
        zle list-choices
    else
        zle expand-or-complete
    fi
}
zle -N complete_pwd_items_on_empty_buffer
bindkey '^I' complete_pwd_items_on_empty_buffer
Community
  • 1
  • 1
4ae1e1
  • 7,228
  • 8
  • 44
  • 77
  • Thanks for your helpful answer. This works exactly like I want it! Great! To your question: because of "cd" the autocomplete function did only show directories. If you go that deep into a directory tree, so that there is no more directory to go to, THEN it showed files (because there was no dir to go to). Is that a better explanation? – stackunderflow Jun 07 '15 at 23:58
  • @stackunderflow I see. I read "only... when..." in another way. – 4ae1e1 Jun 08 '15 at 00:00
  • @stackunderflow But hey, why would completion for `cd` list files when it reaches a leaf? It should return a nonzero status and print "no matches found" (if you've enabled this kind of message) in that case. – 4ae1e1 Jun 08 '15 at 00:02
  • I dont know why it does. But you can try out for yourself. And no, there is no error message. – stackunderflow Jun 08 '15 at 00:07
  • @stackunderflow "I dont know why it does." But it shouldn't. I rely on tab completion for `cd` almost on an hourly basis and know it pretty well. With or without Prezto (without Prezto means vanilla Zsh) I can't reproduce. I guess you have some bad settings in place. – 4ae1e1 Jun 08 '15 at 00:13
  • Here is another more complex variation. It will list **files** on on an empty command line, and in the middle of any command. It will list **directories** on on an empty command line. It will list **executables** on on an empty command line. It can be configured to prepend "cd " or "./" in those cases with a global variable. More details https://ownyourbits.com/2017/01/30/list-files-without-stopping-to-type-in-zsh/. I can't post the code here because it is a closed question – nachoparker Jan 31 '17 at 10:22