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.
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:\> doskeyblog=cdC:\Ben\blogC:\> blogC:\Ben\blog>
It works like a charm! However, if you close and reopen Command Prompt, you'll run into a bit of a problem:
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.
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:
Line 1 @echo offLine 2 doskey blog=cd C:\Ben\blog\Line 3
@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.
Open a new session of Command Prompt, and try out your new aliases!
You're good to go!
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:
Line 1 @echo offLine 2 SETLOCALLine 3Line 4 REM Verify exactly one argument was passedLine 5 if "%~1"=="" goto usageLine 6 if not "%~2"=="" goto usageLine 7Line 8 REM Verify alias doesn't already existLine 9 for /f "tokens=*" %%a in ('doskey /macros:all ^| findstr %1=') do set foundAlias=%%aLine 10 if not "%foundAlias%"=="" goto alias_existsLine 11Line 12 goto add_aliasLine 13Line 14 :usageLine 15 echo USAGE: ad ^<alias^>Line 16 exit /b 1Line 17Line 18 :alias_existsLine 19 echo Alias already exists!Line 20 exit /b 1Line 21Line 22 :add_aliasLine 23, highlighted' echo.doskey %1=cd %cd% >>"C:\Ben\aliases.bat"Line 24, highlighted' call "C:\Ben\aliases.bat"Line 25 echo Alias was added successfully!Line 26 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:\> cdBen\Advent_Of_Code_2019C:\Ben\Advent_Of_Code_2019> adadventAlias was added successfully!C:\Ben\Advent_Of_Code_2019> cd../..C:\> adventC:\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!
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?