This article covers how to alias your directories on Windows. You may be interested in the Unix way.
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
In Windows, you can use the
doskey command to create aliases to use in Command Prompt. Open up Command Prompt and run the following:
C:\> doskey blog=cd 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:
'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.
.bat file. You can call it whatever—
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:
doskey blog=cd C:\Ben\blog\
@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
Open the Registry Editor. Open up the Start menu, and search for regedit. Click the Registry Editor result.
Navigate to the Command Processor settings. This can be found at HKEY_CURRENT_USER → Software → Microsoft → Command Processor.
Add an AutoRun value. Right-click inside Command Processor and choose New → String 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_MACHINE → Software → Microsoft → Command 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!
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
Create a folder to store your scripts in. I called my folder
C:\Ben\Batch, but you can call it
Commandsor any other meaningful name.
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.batinside 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:
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
echo USAGE: ad ^<alias^>
exit /b 1
echo Alias already exists!
exit /b 1
echo.doskey %1=cd %cd% >>"C:\Ben\aliases.bat"
echo Alias was added successfully!
exit /b 0
Replace the paths in lines 23 and 24 with the absolute path to your
Make sure your
aliases.batfile ends in a trailing newline. You'll only need to do this once, unless you manually add stuff to your
aliases.batlater, but this tripped me up for an embarassingly long time.
Open a new session of Command Prompt and alias a directory.
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 ../..
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!
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?