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.

Having a precmd() that does quite a lot, like checking for VCS information, I want to disable it when I'm navigating on slow mounted network drives.

I came up with the following with which I can say slow before changing to a slow drive and fast to change it back, I'm wondering if there is something easier?

precmd_slow() {
    not_much
}
precmd_fast() {
    precmd_slow
    somemore1
    somemore2
}

precmd() {
    precmd_fast
}

slow() {
    precmd() {
        precmd_slow
    }
}

fast() {
    precmd() {
        precmd_fast
    }
}
share|improve this question

2 Answers 2

Not the prettiest solution but I would use just a simple conditional inside the precmd function checking if the current directory is mounted or not. Now doing that depends on what kind of mount it is. If the directories are known, something like this should work.

slowdirs="/home/joe/slowdir1:/home/joe/slowdir2:/mnt/slowdir3"
precmd() {
  isslow=0
  echo $slowdirs | tr \: \\n |
    while read line; do
      if [[ $PWD == $line* ]]; then
        isslow=1
        break
      fi
    done
  if [[ $isslow == 0 ]] ; then
     # do slow stuff

  fi
  # do non slow stuff

}

What this does is test if the current directory starts with (is inside), or is, one of the directories listed in the slowdirs variable. If it isn't one of the "slow" directories, runs the "slow commands", otherwise it skips them.

share|improve this answer

I liked this solution given by chepner on StackOverflow, which I shall quote directly:

In addition to the precmd function, there is also an array called precmd that contains a list of functions to call at the same time. You can manipulate it yourself, but zshcontrib has a function add-zsh-hook to simplify matters.

Code:

# precmd_slow and precmd_fast defined as before

autoload add-zsh-hook
add-zsh-hook precmd precmd_fast

fast () {
    add-zsh-hook -d precmd precmd_slow
    add-zsh-hook precmd precmd_fast
}

slow () {
    add-zsh-hook -d precmd precmd_fast
    add-zsh-hook precmd precmd_slow
}
share|improve this answer

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.