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'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.

share|improve this question
add comment

2 Answers 2

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.

share|improve this answer
add comment

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. :)

share|improve this answer
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.