惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
TaoSecurity Blog
TaoSecurity Blog
博客园_首页
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
量子位
Project Zero
Project Zero
A
Arctic Wolf
小众软件
小众软件
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
K
Kaspersky official blog
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
人人都是产品经理
人人都是产品经理
AI
AI
Cisco Talos Blog
Cisco Talos Blog
MyScale Blog
MyScale Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
S
Schneier on Security
P
Palo Alto Networks Blog

Ben Myers

Tag, You’re It: Blog Questions 2025 Don’t Use aria-label on Static Text Elements Subtitles, Closed Captions, and Open Captions: What’s the Difference? Lost in Translation: Tips for Multilingual Web Accessibility Build a Blogroll with Eleventy I’m a Spotless Giraffe. How I Write Alt Text for Code Snippets on Social Media The Curious Case of “iff” and Overriding Screenreader Pronunciations The Web Needs a Native .visually-hidden Create Shareable Automatic Captions for Live Online Events with Web Captioner A First Look at the Websites and Software Applications Accessibility Act Bill Style with Stateful, Semantic Selectors How I Doubled My Lighthouse Performance Score in One Night How to Fix Your Low-Contrast Text Build a Twitch Chatbot for Sharing Your Content Using Algolia Search Ben’s Humane Guide to Technical Blogging On the ‹dl› Takeaways From “Adapting Comics for Blind and Low Vision Readers: A Roundtable Discussion” Takeaways From Axe-Con 2021 I Finally Understand Eleventy’s Data Cascade. RSS Readers: Yet Another Case for Semantic Markup Implement a Skip Link for Navigation-Heavy Sites aria-label, aria-labelledby, and aria-describedby: What’s the Difference? Out With The Old, In With The New Maintaining Focus Outlines for Windows High Contrast Mode Lexical and Dynamic Scope CSS Can Influence Screenreaders New Year, New Terminal: Alias Your Directories the Windows Way What Is ARIA? The Accessibility Tree How (Not) to Build a Button How Domino’s Could Topple the Accessible Web – Part 1: Public Accommodations
New Year, New Terminal: Alias Your Directories the Unix Way
Ben Myers · 2020-01-01 · via Ben Myers

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
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
if [ -r ~/.bashrc ]; then
    source ~/.bashrc
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
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."
}

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$

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?