50

Googling this didn't show up any results. Here's what I mean: I have a binary file named x in my path (not the current folder, but it is in the PATH), and also a folder with the same name in the current working directory. If I type x, I want the binary to execute, but instead it cd's into that folder. How do I fix this?

Gabi Purcaru
  • 623
  • 1
  • 6
  • 7

2 Answers2

73

TL; DR

Add this line to your ~/.zshrc:

unsetopt autocd

AUTO_CD Option and howto find it

First of all the option you are looking for is AUTO_CD. You can easily find it by looking up `man zshoptions`. Use your pagers search function, usually you press / and enter the keyword. With n you jump to the next occurrence. This will bring up the following:
[..]
   Changing Directories
       AUTO_CD (-J)
              If  a  command is issued that can't be executed as a normal command, and the command is the name of a directory, perform the cd command to that directory.
[..]

The option can be unset using unsetopt AUTO_CD.

Turning it properly off

You are using oh-my-zsh which is described as

"A community-driven framework for managing your zsh configuration" Includes 120+ optional plugins (rails, git, OSX, hub, capistrano, brew, ant, macports, etc), ...

So the next thing is to find out, how to enable/disable options according to the framework.

The readme.textile file states that the prefered way to enable/disable plugins would be an entry in your .zshrc: plugins=(git osx ruby) Find out which plugin uses the AUTO_CD option. As discovered from the manpage it can be invoked via the -J switch or AUTO_CD. Since oh-my-zsh is available on github, searching for it will turn up the file lib/theme-and-appearance.zsh. If you don't want to disable the whole plugin "theme-and-appearance", put a unsetopt AUTO_CD in your .zshrc. Don't modify the files of oh-my-zsh directly, because in case you are updating the framework, your changes will be lost.

Why executables are not invoked directly

Your third question is howto execute a binary directly: You have to execute your binary file via a path, for example with a prefixed `./` as in `./do-something`. This is some kind of a security feature and should not be changed. hing of plugging in an USB stick, mounting it and having a look on it with `ls`. If there is a executable called `ls` which deletes your home directory, everything would be gone, since this would have overwritten the order of your $PATH.

If you have commands you call repeatedly, setting up an alias in your .zshrc would be a common solution.

wim
  • 103
  • 4
echox
  • 17,753
  • 7
  • 51
  • 56
  • 5
    Thank you very much! What I needed was the `unsetopt auto_cd` line (I did explicitly mention that the executable _is_ in my path though; nevertheless, the explanation might help other users figure things out). – Gabi Purcaru Apr 27 '14 at 10:06
  • Thank you for the thorough answer and providing details on how you found the information (thus helping others find information like this on their own in the future). – trailing slash Sep 08 '16 at 03:30
  • 3
    Your last paragraph makes no sense. The OP is asking about a binary in their `PATH` environment variable which they want to take precedence over autocd. It has nothing to do with shell script's inability to run an executable from a path without specifying either `./` or `/`. – Qix - MONICA WAS MISTREATED Oct 12 '16 at 22:48
  • The first link to the readme does not work. [Here](https://github.com/robbyrussell/oh-my-zsh) it is. Furthermore, the OMZ `lib` directory has nothing todo with `plugins`.Regarding the question and as you said, the correct command is `unsetopt`. – Timo Oct 23 '17 at 08:14
  • This is one of the least useful technically correct answers I've ever seen. So much *completely* unnecessary and useless context when you could have just said "`lib/theme-and-appearance.zsh` is responsible for turning that feature on. Add `unsetopt AUTO_CD` to your .zshrc to disable it." – iono Jun 16 '22 at 21:55
26

This worked for me:

unsetopt autocd
Eyal Levin
  • 401
  • 4
  • 3