For those of us that use the terminal on a daily basis, sometimes even switching between projects throughout the day, it can be a quite repetitive and tedious task to have to open a terminal window, drill down to the correct project directory using
cd, and then run some command to either boot your vagrant virtual machine or build your assets using webpack or gulp for example.
That repeating process can become even more annoying if you have a variety of projects, some legacy and others cutting-edge, making it hard to remember which command to run, with what parameters, and in what order.
Bash aliases to the rescue
Something I've been doing for as long as I can remember is to create a bash alias for every project in order to automate that entire process.
For example, to navigate into a project's directory, spin up its virtual machine, build the project's front-end assets, and start watching for changes, you can add the following alias to the
~/.bash_profile (on a Mac) or
~/.bash_aliases (for Linux) file:
alias dev-myproject='cd /path/to/your/project && vagrant up && yarn run build && yarn run watch'
Now you can just type
and you're good to go in no time!
For convenience, I prefix all of them with
dev- so I can simply open a terminal window, type
dev-, and hit tab to autocomplete and show a list of all quick-start projects. This saves me a tremendous amount of time and headaches compared to typing out that entire command every single time I want to work on a project.
Tackling other repetitive tasks
The same goes for other repetitive, more generic tasks like bringing up a vagrant machine and SHH'ing into it:
alias vgo="vagrant up && vagrant ssh"
Now you can type
vgo and you're ready to go! Use
vhalt to quickly end your session when you're done working:
alias vhalt="vagrant halt && exit"
Of course your options are endless and are not limited to booting a project's environment. For example, you can create shortcuts for your most used commands:
alias pa='php artisan' alias cda='composer dumpautoload' alias yr='yarn run'
Do you have some other examples of shortcuts or neat bash alias tricks? Share them in the comments!