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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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. – AProgrammer Nov 11 '11 at 14:42

I think you just described AUTO_CD.

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

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.

share|improve this answer

Your Answer


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.