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

推荐订阅源

Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
美团技术团队
博客园 - 聂微东
博客园 - 叶小钗
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
Spread Privacy
Spread Privacy
J
Java Code Geeks
雷峰网
雷峰网
宝玉的分享
宝玉的分享
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
量子位
L
LINUX DO - 热门话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
腾讯CDC
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
罗磊的独立博客
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
Project Zero
Project Zero
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
MyScale Blog
MyScale Blog
T
The Exploit Database - CXSecurity.com
GbyAI
GbyAI
博客园 - 【当耐特】
O
OpenAI News
Schneier on Security
Schneier on Security
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
博客园 - 司徒正美

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 Unix 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 Windows Way
Ben Myers · 2020-01-01 · via Ben Myers

This article covers how to alias your directories on Windows. You may be interested in the Unix 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 with Doskey

You’ll want to pick a frequently used directory and a memorable command you’ll use to hop to that directory. In my case, I want to run the blog command to go to C:\Ben\blog.

In Windows, you can use the doskey command to create aliases to use in Command Prompt. Open up Command Prompt and run the following:

Command Prompt
C:\> doskey blog=cd C:\Ben\blog
C:\> blog
C:\Ben\blog>

It works like a charm! However, if you close and reopen Command Prompt, you’ll run into a bit of a problem:

Command Prompt
C:\> blog
'blog' is not recognized as an internal or external command,
operable program or batch file.

doskey aliases don’t persist between sessions. Instead, we have to put them in a persistent batch file.

Persisting Doskey Aliases

Whereas Unix has .bashrc that runs with every new terminal session, Windows has no such files. We’ll need to create our own.

Create a .bat file. You can call it whatever—aliases.bat, scripts.bat, doskey.bat…—so long as it works for you. I’ll call mine aliases.bat and place it in the home directory.

Inside this batch file, I’ll put:

aliases.bat
@echo off
doskey blog=cd C:\Ben\blog\

(That @echo off is to make sure the terminal doesn’t vomit out the whole aliases.bat file whenever you start a new session.)

The next step is making sure Command Prompt knows to run your batch file whenever you start a new terminal session. To do this, we need to make a change to the Windows Registry—your Windows machine’s operating system-level configurations. We’ll add a configuration that specifies that whenever Command Prompt starts a new session, it should automatically run aliases.bat.

  1. Open the Registry Editor. Open up the Start menu, and search for regedit. Click the Registry Editor result.

  2. Navigate to the Command Processor settings. This can be found at HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor.

  3. Add an AutoRun value. Right-click inside Command Processor and choose NewString Value. Give the new property the name AutoRun. Make the value the absolute path to your batch file of aliases.

Couldn't find the Command Processor settings?

It’s possible they’re inside HKEY_LOCAL_MACHINESoftwareMicrosoftCommand Processor instead! If you still can’t find your settings, check out this Stack Overflow question for support.

Open a new session of Command Prompt, and try out your new aliases!

Command Prompt
C:\> blog
C:\Ben\blog>

You’re good to go!

Adding Persistent Doskey Aliases on the Fly

I like having an alias for just about any directory or workspace I might come back to. Manually modifying aliases.bat and restarting my terminal every time I create a directory would interrupt my flow, however. Instead, I have a batch script that automatically creates a persistent doskey alias to the current working directory whenever I use the ad command.

  1. Create a folder to store your scripts in. I called my folder C:\Ben\Batch, but you can call it Scripts or Commands or any other meaningful name.

  2. Add your scripts folder to your PATH. If you haven’t done this before, check out Ryan Hoffman’s quick guide. When you run an unfamiliar command, the terminal checks all directories listed in the PATH to see whether any of them have a script or executable file with the same name. For instance, if you run ad, the terminal checks all directories in the PATH for an executable file called ad.

  3. Create an ad.bat inside your scripts folder. By calling this file ad.bat, you ensure that the file is executed whenever you run the command ad. If you’d prefer to use a different command, you can choose a different name. Paste the following into your new batch file:

ad.bat
@echo off
SETLOCAL

REM Verify exactly one argument was passed
if "%~1"=="" goto usage
if not "%~2"=="" goto usage

REM Verify alias doesn't already exist
for /f "tokens=*" %%a in ('doskey /macros:all ^| findstr %1=') do set foundAlias=%%a
if not "%foundAlias%"=="" goto alias_exists

goto add_alias

:usage
echo USAGE: ad ^<alias^>
exit /b 1

:alias_exists
echo Alias already exists!
exit /b 1

:add_alias
echo.doskey %1=cd %cd% >>"C:\Ben\aliases.bat"
call "C:\Ben\aliases.bat"
echo Alias was added successfully!
exit /b 0
  1. Replace the paths in lines 23 and 24 with the absolute path to your aliases.bat.

  2. Make sure your aliases.bat file ends in a trailing newline. You’ll only need to do this once, unless you manually add stuff to your aliases.bat later, but this tripped me up for an embarassingly long time.

  3. Open a new session of Command Prompt and alias a directory.

Command Prompt
C:\> cd Ben\Advent_Of_Code_2019
C:\Ben\Advent_Of_Code_2019> ad advent
Alias was added successfully!
C:\Ben\Advent_Of_Code_2019> cd ../..
C:\> advent
C:\Ben\Advent_Of_Code_2019>

I’m still very new to batch scripting, so if you find a problem with this script or a way to improve it, please reach out!

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?