DEV Community

Cover image for New Year, New Terminal: Alias Your Directories the Unix Way
Ben Myers
Ben Myers

Posted on • Originally published at blog.benmyers.dev

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

This article covers how to alias your directories on Unix, and was originally published on my blog. Additionally, 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$
Enter fullscreen mode Exit fullscreen mode

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
~$
Enter fullscreen mode Exit fullscreen mode

... 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
~$
Enter fullscreen mode Exit fullscreen mode

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

We can drop our new alias in and save:

alias blog="cd ~/blog"
Enter fullscreen mode Exit fullscreen mode

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

~$ source ~/.bashrc
~$ blog
~/blog$
Enter fullscreen mode Exit fullscreen mode

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:

if [ -r ~/.bashrc ]; then
    source ~/.bashrc
fi
Enter fullscreen mode Exit fullscreen mode

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:

function ad() {
    if [[ "$#" -ne 1 ]]
    then
        echo "USAGE: ad <alias>"
        return 0
    elif [[ -n "$(alias $1 2>/dev/null)" ]]
    then
        echo "Alias already exists!"
        return 0
    fi

    echo -e "alias $1=\"cd $(pwd)\"" >> ~/.bashrc
    source ~/.bashrc
    echo "Alias was added successfully."
}
Enter fullscreen mode Exit fullscreen mode

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$ ad advent
Alias was added successfully.
~/codebase/Advent_Of_Code_2019$ cd ~
~$ advent
~/codebase/Advent_Of_Code_2019$
Enter fullscreen mode Exit fullscreen mode

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?

Latest comments (7)

Collapse
 
syntaxseed profile image
SyntaxSeed (Sherri W)

Ooo I'm using this now. Thanks.

Collapse
 
syntaxseed profile image
SyntaxSeed (Sherri W)

For anyone who wants a matching function to remove the aliases...

function del_alias() {
    if [[ "$#" -ne 1 ]]
    then
        echo "USAGE: del_alias <alias name>"
        return 0
    elif [[ -n "$(alias $1 2>/dev/null)" ]]
    then
        sed -i "/alias $1=/d" ~/.bashrc
        source ~/.bashrc
        echo "Alias removed."
        return 0
    fi
    echo "Alias does not exist."
}
Collapse
 
bendmyers profile image
Ben Myers

This is neat. Thanks for sharing!

Collapse
 
darkwolf7 profile image
DarkWolf7

In most of the places I've worked the systems are very locked down and many are running Solaris Unix or AIX, so downloading libraries or compiling stuff from the web is not an option unless you want to be walked out, so using aliases like this is far more useful than z or goto (excellent tools! But only an option @ home).

How locked down, you ask? My office defaults you to ksh and the only way to change that is to have exec bash in my .profile

Collapse
 
jsardev profile image
Jakub Sarnowski

Why not just use z?

Collapse
 
bendmyers profile image
Ben Myers

I hadn't heard about this before. This is really cool! Thank you for sharing!

Collapse
 
jsardev profile image
Jakub Sarnowski

Yeah it's an amazing tool! I can't live without it πŸ˜„ You won't ever need directory aliases again!