Here’s a shell cd rewritten to pander to my usual workflow. It adds context sensitivity to the normal no arguments behaviour of cd.

In your $HOME/.bashrc


# make sure you do this. If you don't, kittens may die
# and on OS X textmate might stop working proper-like
if [[ $- != *i* ]] ; then
    # Shell is non-interactive.  Be done now!
    return
fi

function cd () {
  local goto=$*

  if [[ $# == 0 ]]; then
    if [[ "$(pwd)" =~ "^${HOME}/dev/([^\/]+)" ]]; then
      goto="${HOME}/dev/${BASH_REMATCH[1]}" 

    elif [[ ! ( $(pwd) =~ "^${HOME}" ) ]]; then
      goto=`cat ~/.lastpwd`

    else
      goto=$HOME
    fi
  fi

  builtin cd "$goto" 

  if [[ "$(pwd)" =~ "^${HOME}" ]]; then
    pwd > ~/.lastpwd
  fi
}

builtin cd `cat ~/.lastpwd`

Rules

I’m still mucking around with the rules, but here they are at the moment:

open a new shell

Change to the last directory you were in under $HOME.

type cd dirname

Changes to that directory as normal. If the directory is within $HOME, record it.

type cd (no args)

  • If you’re somewhere below $HOME/dev/projectname, change to $HOME/dev/projectname
  • If you’re somewhere else under $HOME, cd $HOME
  • If you’re outside of $HOME, change back to the last directory you were in under $HOME

1 Response to “Clever Clogs cd”

  1. Lachie Says:
    Test testsett

Leave a Reply