Ben Myers

New Year, New Terminal: Alias Your Directories the Unix Way

This article covers how to alias your directories on Unix. You may be interested in the Windows way.

Introduction

I admit it. I'm a sucker for creating little shortcuts and scripts to speed up work in my terminal. There's just something oddly thrilling about typing a few characters and kicking off several commands. I recently read Chiamaka Ikeanyi's Avoiding Shell Hell: Aliases to the Rescue, and I was inspired to share some alias tricks I use on a daily basis.

My team at work manages six projects. Each project has its own repository. Additionally, we have repositories for developer tools made by the team. Combine that with any other directories we use on a daily basis, and cd quickly becomes our most frequent command.

My favorite terminal trick, for both my home and work computers, is creating short, memorable aliases to cd to my most frequent directories. If I want to write a new post, I just type blog and I'm in my Gatsby codebase. If I need to tweak the response from a mock server, I type mock, and I can start poking around my Express.js code. I rarely have to worry about long, complex relative paths. The terminal feels snappier, more intuitive, and—best of all—more fun.

Creating Aliases

Pick a directory you use often, and a memorable alias you'll use to hop to that directory instantly. In my case, I want to go to my ~/blog directory whenever I use my blog alias.

Let's give it a shot! In your terminal, run the following commands:

~$ alias blog="cd ~/blog"
~$ cd some/other/directory/
~/some/other/directory$ blog
~/blog$

No matter which working directory we're in, issuing the blog command now takes us directly to ~/blog. Mission accomplished! We can close our terminal and call it a day.

Next time, we can just open up our terminal and...

~$ blog
-bash: blog: command not found
~$

... oh.

Aliases only last as long as the terminal session. Since manually reestablishing our aliases every time we open the terminal would be a bit of a hassle, let's find a way to make our aliases persist.

Persisting Bash Aliases

When you start a new interactive shell, your terminal runs a file called .bashrc, found in your root directory. Open ~/.bashrc in your editor of choice. I'm using VS Code, but you could also use vi, emacs, nano, Atom, or whatever other editor floats your boat:

~$ code~/.bashrc
~$

(If .bashrc doesn't exist, go ahead and create it!)

We can drop our new alias in and save:

.bashrc
Line 1 alias blog="cd ~/blog"

Back in our terminal, we tell our terminal to rerun .bashrc and receive our new aliases:

~$ source ~/.bashrc
~$ blog
~/blog$

In future terminal sessions, you won't even have to run source, since the terminal takes care of that for you. You'll be able to just run blog to your heart's content.

But Wait! I Have a Mac!

The default macOS Terminal inexplicably treats every terminal session as a login session. This means that instead of running ~/.bashrc on every session, the macOS Terminal runs ~/.bash_profile. Cue facepalm.

You can account for this by just stuffing your aliases in .bash_profile. However, if you think you might want to use a different terminal at some point, the more resilient approach would be to have your .bash_profile source .bashrc on every login:

.bash_profile
Line 1 if [ -r ~/.bashrc ]; then
Line 2  source ~/.bashrc
Line 3 fi

For more information, read this handy Scripting OS X guide to .bash_profile and .bashrc.

Adding Bash Aliases on the Fly

We can use Bash scripting to be even cuter about aliasing our directories. I like having an alias for just about any directory or workspace I might come back to. Manually modifying .bashrc and re-sourcing every time I create a directory would interrupt my flow, however. Instead, I have a quick script that automatically creates a persistent alias to the current working directory whenever I use the ad command.

Copy the following into your ~/.bashrc:

.bashrc
Line 1 function ad() {
Line 2  if [[ "$#" -ne 1 ]]
Line 3  then
Line 4  echo "USAGE: ad <alias>"
Line 5  return 0
Line 6  elif [[ -n "$(alias $1 2>/dev/null)" ]]
Line 7  then
Line 8  echo "Alias already exists!"
Line 9  return 0
Line 10  fi
Line 11 
Line 12, highlighted'  echo -e "alias $1=\"cd $(pwd)\"" >> ~/.bashrc
Line 13, highlighted'  source ~/.bashrc
Line 14  echo "Alias was added successfully."
Line 15 }

The interesting lines are 12 and 13 — the rest is just sanity checks.

Let's give our new ad command a whirl! If you're using an old terminal session, update your terminal's aliases with source ~/.bashrc. Then try using ad:

~$ cd ./codebase/Advent_Of_Code_2019
~/codebase/Advent_Of_Code_2019$ adadvent
Alias was added successfully.
~/codebase/Advent_Of_Code_2019$ cd ~
~$ advent
~/codebase/Advent_Of_Code_2019$

ad has so thoroughly integrated itself into my day-to-day development work that I don't often create a new directory without instantly creating an alias for it.

Conclusion

Tweaking my terminal makes programming a more delightful experience for me, and it can for you, too. By aliasing cd commands to your most frequently used directories, you cut down on having to juggle potentially long absolute or relative paths. Using the terminal becomes faster, more intuitive, and personal. What's not to love?


Ben Myers

Ben Myers is a human T-rex, software developer, accessibility advocate, and a passionate educator. He graduated from Oklahoma State University in 2018 with a bachelor's degree in Computer Science, and now works for USAA as a full-stack engineer. Check out his portfolio and connect with him on LinkedIn.