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.

When using cd to navigate to a path found by cdpath that is not located relative to ., the resulting path gets echoed. For example, assuming a directory structure like ~/parent/child and a cdpath like cdpath(~/parent), you see this:

$ cd parent
$ cd ~
$ cd child
~/parent/child
$

Basically, I want cd child to be silent, just like cd ~/parent/child, regardless of distance from the current directory. Is there a way to silence cd when it is using cdpath to locate and jump to a directory?

share|improve this question
    
@Scott thanks for the cleanup–made my own question make more sense to myself. –  lettertwo Mar 13 '13 at 21:07

2 Answers 2

up vote 1 down vote accepted

I think, that is hard-coded into the shell. Here's an excerpt from builtin.c:

if (isset(INTERACTIVE) || isset(POSIXCD)) {
    if (func != BIN_CD && isset(INTERACTIVE)) {
        if (unset(PUSHDSILENT) && !quiet)
            printdirstack();
    } else if (doprintdir) {
        fprintdir(pwd, stdout);
        putchar('\n');
    }
}

int doprintdir is kind of a flag, which is set when using e.g. the cdpath.

So, if you really want to get rid of the printing of the new dir, comment out the else if part in the source -- with the disadvantage that you'll never get noticed anymore.

share|improve this answer
    
Ah, i was afraid this was the case. Thanks for digging that up. –  lettertwo Mar 13 '13 at 21:06

One approach would be to roll your own cd function:

cd()
{
    if [ $# = 0 ]
    then
        builtin cd "$HOME"
        return
    fi
    # Use       if [[ "$1" =~ ^/ ]]
    # here if zsh supports it.
    case "$1" in
      /*)
        builtin cd "$1"
        return
    esac
    if [ -d "$1"  -a  -x "$1" ]
    then
        builtin cd "$1"
        # You might want this to return only if the cd succeeds.
        return
    fi
    for base in ~/parent /other/dir/thatIuse_a_lot
    do
        if builtin cd "$base/$1" 2> /dev/null
        then
            return
        fi
    done
    echo "$1: cd failed" >&2          # Customize to your liking.
    return 1                          # Indicate failure.
}

I don’t have access to a zsh right now, but I tested this in the Korn shell.  Of course you can enhance that to parse a cdpath variable at run time; I’ll leave that as an exercise.

share|improve this answer
    
I considered this, but the idea of clobbering a builtin (especially one so central to shell interactions as cd) scared me off. Still, lacking any alternatives (short of modifying the actual builtin), a truly robust version of this might make a good oh-my-zsh contribution… –  lettertwo Mar 14 '13 at 15:48

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.