Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use zsh and oh-my-zsh for my shell environment on OS X, and I'm having a hard time tracking down whether or not the thing that is happening is a result of an oh-my-zsh function or the default behavior of zsh; it's entirely a cosmetic issue but it drives me crazy.

The short version is that if I cd in to a directory, the thing that is printed in my prompt from %c is exactly the argument that was passed in to cd; so if I'm changing in to the directory "test" but I spell it "tEst" by accident, then "tEst" is what will show up in my prompt. This is very annoying.

Another example of where it pops up is when I use this shell function (simplified from Brett Terpstra)

cdf()
 {
   target=`osascript -e 'tell application "Finder" to get POSIX path of (target of front Finder window as text)'`
   cd ${target}
 }

When I use this function, regardless of what directory I end up in, my working directory is displayed as ~target in my prompt.

Like I said, these are totally cosmetic issues, but it's driving me crazy. Is this a zsh thing, an oh-my-zsh thing, or is there a different expansion I can use in my prompt settings that would fix this? I'm using iTerm 2 but I've tried it in Terminal.app and the same thing happens.

share|improve this question
    
Does it work better as %1~ instead of %c? –  Bradd Szonye Apr 18 '13 at 2:14
    
@BraddSzonye behaves the exact same. –  Doug Stephen Apr 18 '13 at 2:34
    
Hm, what does $PWD look like when you cd into these directories? You might be able to use that instead of %c in your prompt, perhaps after some shell substitution processing. –  Bradd Szonye Apr 18 '13 at 6:04
    
@BraddSzonye echo $PWD spits out the same :-/ –  Doug Stephen Apr 18 '13 at 15:03
    
OK, that's just not right. I have an idea, let me do a little more research when I'm at my Unix box. –  Bradd Szonye Apr 18 '13 at 18:50

1 Answer 1

up vote 2 down vote accepted

The two parts of your question have different resolutions.

cd TEst

This behavior results from the interaction of two quirks:

  • OS X uses a case-insensitive file system by default.
  • Shells set PWD to the "current working directory as set by the cd command," as opposed to the absolute path reported by the getcwd system call.

The latter quirk allows you to use symlinks as if they were normal directories. By remembering the path you typed instead of the absolute path, the shell lets you cd symlink ... cd .. to get back to where you started. On a case-insensitive file system, typos like cd TEst behave just like symlinks, setting PWD to the path you typed instead of the directory's filesystem name.

To work around this, you can use $(pwd -P) to get the canonical path as reported by getcwd instead of the shell's logical path. Beware that this has the side effect of resolving all symlinks in the path. To mimic the formatting of %c use: $(basename $(pwd -P | sed "s#$HOME#~#"))

Alternately, you could convert your file system to case-sensitive, but some OS X applications like Steam and Photoshop are not compatible.

cd ${target}

I cannot reproduce this behavior in OS X with plain zsh – it may be a problem with oh-my-zsh or your configuration. While you can use the same $(pwd -P) workaround for this, other scripts and programs may fail if $PWD does not resolve to a legitimate path. If you can reproduce the problem with a minimal oh-my-zsh installation, you should report a bug to the maintainers.

share|improve this answer
    
Regarding the whole ${target} thing, this is not an issue if I use PWD, it's only an issue if I use things like %c. But using pwd in my prompt shows the entire path instead of just the name of the current directory. I sort of prefer the succinctness of %c but using pwd might be tolerable. –  Doug Stephen Apr 19 '13 at 13:45
    
So, I ended up making a compromise; I'm not gonna worry so much about the case sensitivity stuff yet, but in order to fix the ${target} thing, I've decided to use the following in my prompt: $(basename `pwd` | sed 's/dstephen/\~/') –  Doug Stephen Apr 19 '13 at 16:04
    
Good solution. I will add the basename & sed parts to my answer, since it's a closer replacement to %c. If you don't care about ~ replacement, you can simplify that to: ${PWD##*/} –  Bradd Szonye Apr 19 '13 at 20:09
    
Awesome. I like the ~ replacement, and I left out the -P option because sometimes my symlinks don't have the same name as what they link (semantically nice name for an ugly directory) to and I prefer the symlinked names to be displayed in the prompt. –  Doug Stephen Apr 19 '13 at 20:53

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.