Using Terminal aliases to easily switch project environments

If you work for an agency, chances are you have multiple development environments. Some clients have you mirroring their engineering team’s setup, others you created to your preference, and yet more are relics of past configurations you’ve forgotten about.

In this situation, I have a hard time remembering which project needs a “bundle exec guard” versus an “npm start” to get my SASS compilers, linting tools, and other gems going. Lately, I’ve been enjoying the use of bash aliases to both act as a more organized system of inventory, and to maintain my sanity when toggling between development environments.

I’ve come up with a naming system that makes sense for me and it consists of two basic parts:

  1. What am I doing?
    For this, I have prefixes cd- for jumping to directories, start- for executing a series of commands to begin development, or nano- to open and edit configuration files (particularly those I don’t access often and forget URLs for).
  2. To what project/file am I doing it?
    That cd- then gets combined with shorthand for a client and project pair. For example, cd-a-intranet to replace “cd /Tree/Acme/acme-intranet/”. I like to have access to a client root as well, so I also define cd-acme.

Adding your first aliases

To begin creating aliases for your projects, edit your .bash_profile with Terminal command:

Last login: Thu May 14 18:25:42 on ttys000
adp:~ anthonydpaul$ nano ~/.bash_profile

Beginner aliases as I described above are each added on a new line:

alias cd-acme='cd /Library/WebServer/Documents/Clients/Acme'

alias cd-a-intranet='cd /Library/WebServer/Documents/Clients/Acme/acme-intranet'

alias start-a-intranet='bundle exec guard'

After editing, follow the instructions to save and exit. Your bash profile is only loaded once at the beginning of a new Terminal session, so you’ll need to relaunch Terminal to test your new aliases.

Now for the fun part

I’ve given you everything you need to know to quickly jump around project folders and to start a variety of Terminal environments to begin development, but remember when I said I didn’t like opening Finder? You can have Terminal do that for you too.

Depending on how your project’s environment is configured, it likely falls into one of these camps:

  1. Flat files, no host/server – Just a folder of HTML files with no virtual host or server.
  2. A host/server with no frills – No watchers, guards, or other fancy tools, but a host URL, including virtual hosts.
  3. Frills like active watchers and compilers – Something like Guard (a Ruby gem) running that creates a dynamic host URL and port on command.

For all of those, you can modify your start- alias to include some added open commands to automagically open your Finder window and launch your project in your web browser—all as part of your start process.

Open your Finder directory:

open .

Open an HTML file without a virtual host:

open "/Library/WebServer/Documents/Clients/Acme/acme-intranet/index.html"

Open a URL (e.g., a virtual host):

open "http://acme-intranet/index.php"

For something like Guard, you don’t actually need to do anything. It likely opens your browser windows already.

Open some other URL (e.g., GitHub Tickets assigned to you):

open ""

Use those in tandem, within your start- alias:

(Each command should be on a new line, within the quotes around the entire alias string.)

alias cd-acme='cd /Library/WebServer/Documents/Clients/Acme'
alias cd-a-intranet='cd /Library/WebServer/Documents/Clients/Acme/acme-intranet'
alias cd-a-minisite='cd /Library/WebServer/Documents/Clients/Acme/acme-minisite'
alias cd-a-investors='cd /Library/WebServer/Documents/Clients/Acme/acme-investors'

alias start-a-intranet='open .
open "http://acme-intranet/index.php"
open ""
npm start'

alias start-a-minisite='open .
open "/Library/WebServer/Documents/Clients/Acme/acme-minisite/index.html"'

alias start-a-investors='open .
bundle exec guard'

Open editing windows

Pair your Finder folder and browser window open commands with opening a handful of files in the editor of your choice. Use an asterisk to give a variable, particularly for prefixed project files within a larger repo (i.e., minisite.*.php).

alias start-a-intranet='open .
open -a TextWrangler index.php scss/*.scss js/scripts.js
open "http://acme-intranet/index.php"
npm start'

Now you have the tools to quickly cd-a-intranet and start-a-intranet, opening everything you need when your server starts up—all with two quick keyboard commands.

As a closing tip, it’s a bit meta, but I also have a few aliases for remembering where my bash profile is, alongside those other host files I need to edit when I add a new project. These will be easier to remember when you haven’t used them in six months:

alias nano-alias='nano ~/.bash_profile'
alias nano-aliases='nano ~/.bash_profile'
alias nano-hosts='sudo nano /etc/hosts'
alias nano-vhosts='sudo nano /etc/apache2/extra/httpd-vhosts.conf'

Are you doing something else with aliases you think I’d be interested in? Share it in the comments!