When I type something which is neither a zsh builtin and no such executable is found from $PATH, zsh just reports an error. Instead, I would like zsh to check if a named directory exists with that name and cd into it.

I tried defining command_not_found_handler() function but it didn't work as it forks a sub-shell to execute that function and hence directory change is not reflected in the actual shell.

Is it something that is already possible with some settings or a new (useful?) feature?

  • Do you have defined your function with () or {} around the body. If I'm not mistaken, with {} zsh shouldn't launch a subshell to execute the command not found handler. Nov 11 '11 at 14:42

The zsh manual states:

If no external command is found but a function command_not_found_handler exists the shell executes this function with all command line arguments. (…) Note that the handler is executed in a subshell forked to execute an external command, hence changes to directories, shell parameters, etc. have no effect on the main shell.

So you can't do what you described short of patching zsh.

You could create a function for each of your named directories.


I think you just described AUTO_CD.

  • 1
    Just as a note, shopt -s autocd is also available in bash so it is not a zsh specific thing.
    – jw013
    Nov 11 '11 at 9:43
  • No, I'm not looking for this. I don't want to cd into a subdirectory. I want to cd to a "zsh named directories". Eg: cd ~mydir.
    – balki
    Nov 11 '11 at 13:07

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.