6

I'm new to zsh and one thing that I do quite often is execute shell scripts that are in a directory of mine called Scripts. I'm not always inside of the Scripts directory, however... I may be in $HOME and therefore would like to just type Sc<Tab> and have it autocomplete to Scripts/ and then be able to autocomplete which script I want to run from there, of course...

However, zsh doesn't seem to do this by default. It will only autocomplete directory names for me if I have typed ls or cd. If I'm trying to autocomplete a directory first, it just simply won't do it.

Is there a way to change this behavior?

Edit: I figured out that I could do ./Sc<Tab> and it would autocomplete for me. I guess I'm just used to not having to type out ./ before something to get it to autocomplete, so I'd still like to know if this is possible in zsh, but this is an acceptable workaround for now.

4

Not exactly what you asked but I use s to run scripts. So for example if I have a file ~/Scripts/run-stuff.sh I can type s run-stuff.sh and it will run the script. It will also do tab-completion of the script name. The only thing it doesn't do is handle sub-directories in the scripts directory.

# Completion for s (scripts)
_s_comp() {
    if [ $OS = "mac" ]; then
        local LS=gls;
    else
        local LS=ls;
    fi
    reply=(`$LS --ignore-backups $HOME/Scripts/`);
}

compctl -K _s_comp s

It's possible that you may also need an alias like alias s="$HOME/Scripts

Edit: note that this will work regardless of what directory you are currently in.

3

You can add this instruction to your ~/.zshrc:

setopt autocd

It also has a beneficial side effect of not having to type cd to change directories. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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