Bash aliasing for fun and profit

Today we're going to do a little bit of personal devops, streamlining our own development process, reducing tedium, and getting to the good stuff faster. What's that, you like all those things too? Great! Let's get started!

The topic is bash aliasing, which you may or may not have heard of; no worries either way, we'll cover the basics below.

Small caveat that there are several different shell terminals out there; I use ZSH but the process is not very different if you use bash or another shell, and I'll try to make it as agnostic as possible.

Let's assume for the sake of the demonstration that I am working on an app which requires me to switch Node versions; I run 12.x under normal circumstances, but this app requires 8.x to work properly. I use nvm to manage my different versions, but each time I close my terminal I have to nvm use 8.x.x again before I can run this application.  Let's further assume that the application has a really long, obnoxious folder name, something like the_longest_folder_name_in_the_whole_wide_world. Maybe the application files live in a subfolder of that, so to get there I'd have to type cd the_longest_folder_name_in_the_whole_wide_world/app_data.

Every. Single. Time.

Luckily, there's a better way. (Sure, you could organize your folders not to have such Tikki-Tikki-Tembo-esque names, but let's assume for the sake of our demonstration that you need them that way.)

OK, let's open up our config file:

# zsh
vi ~/.zshrc
# bash
vi ~/.bashrc
# or you can open it with your code editor of choice (mine's Atom)
atom [~/.zshrc || ~/.bashrc]

Then, we'll set up an alias called long-app:

alias long-app="cd && nvm use 8.x.x && cd the_longest_folder_name_in_the_whole_wide_world/app_data && npm start"

And that's it; just put that line in the config file somewhere, save it, restart your terminal. Then type long-app and watch the magic happen. To step through it here: 

  1. cd takes me to the root folder from wherever I happen to be.
  2. nvm use 8.x.x swaps out the current version of Node for the one I need. 
  3. cd the_longest... takes me to the application folder.
  4. Finally, npm start runs my app.

I guarantee you will end up saving tens of hours of lifespan with a few choice aliases. A couple of my other daily time-savers:

  • gco for git checkout
  • 8 for nvm use 8.x.x
  • omdebug sets some command-line debugging variables before starting up the main app I'm working on, 7 characters replacing about 60, nearly 90% savings! Can't beat that.

Well that's it. Now sally forth and save yourself some keystrokes, champions!