Smart Start Bash FunctionPosted on: May 11, 2016

You have two choices:

  1. Use a long, sometimes convoluted command for starting up your apps from the command line.
  2. Simply type start no matter which project you're currently working on.

For me, the answer is obvious. All I need to do to fire up any project requires me to simply type a single word and I'm up and running.

Yes... it's definitely a micro-optimization. The reduction in cognitive load is minimal and you can get away without it using a quick ctrl+r history lookup.

Regardless... I hope this makes someone else's life just that tiny little bit easier.

The script

Just stick this into your .bash_profile/.zshrc/ wherever you put your aliases, and modify it to suit your needs.

#===== Smart, directory-aware start function =====#
function start {
  if [[ $PWD == *remy.bach.me.uk* ]] then
    docpad run --env static
  else
    echo "You don't seem to be in a folder/project that I know how to start up."
  fi
}

Now if I'm in ~/projects/remy.bach.me.uk I can run start and it will fire off docpad run --env static for me.

Don't forget to try this in a new tab after saving, or run source ~/.zshrc to make sure your function works.

One step further

function start {
  if [[ $PWD == *rails-project* ]] then
    if [[ "$1" == unicorn ]] then
      be unicorn -p 9000
    else
      be rails server
    fi
  else
    echo "You don't seem to be in a folder/project that I know how to start up."
  fi
}

Now I have the option to run start unicorn to spin up the same project a different way should I need to.