Using NodeJS because DOSKEY hurts
So I've tried for a few years now to enrich my experience at the windows command line by making it more unix-like.
Specifically, I wanted a way to create persistent aliases, which lead me to DOSKEY
, a windows command-line tool that can create shortcut commands. Notice how I said "shortcut"? Yeah, I've had experience with bash's alias
and DOSKEY
feels severely lacking - and so I was about to give up.
But recently I realised, that you can register cross-platform commands in a package.json
file for an NPM project. The way it works is by NPM having a folder, within which, it creates small files with .cmd
, .sh
and {filename}
whenever an NPM package registers a command. This folder's location is added to the path so these files can be run directly from the command line. Interesting... Let's exploit this same process to make the Windows Command Prompt a little nicer - particularly for aliases.
Obivously, the only pre-requisite is that you have Node installed (it doesn't even need to be on the path!)
(Not-Quite-A*) Pathing
The first thing to set is the location of where we're going to place our alias files. To make this more portable, use something that's common across all accounts:
mkdir %USERPROFILE%\.aliases
setx ALIAS_PATH "%USERPROFILE%\.aliases"
setx PATH "%PATH%;%ALIAS_PATH%"
setx
is the permanent version of set
. You can also edit the PATH
graphically, but install scripts are more fun. We're persisting our alias path because I have a hunch that we might need it later in the future... Even if we dont, it can't hurt to have it.
The first alias!
Now that the folder is on the path, we can place files in here to run them without needing the directory. The first alias I made was ls
, just to see if it works, but also because I make the mistake when I switch back to Windows:
ls.cmd
:: Just an alias for dir /B. /B makes it bare (no header info)
@dir /B %*
There's no need to reload your terminal, too! You should be able to > ls
right away and see the output! Not quite the unix ls
, but at least it works!
In fact, since we made our alias path an environment variable, we can exploit the fact that we can make aliases from anywhere!
> cd
C:\Users\User\tmp
> echo :: Alias for move >> %ALIAS_PATH%\mv.cmd
> echo @move %* >> %ALIAS_PATH%\mv.cmd
> echo test >> a.txt
> ls
a.txt
> mv a.txt b.txt
1 file(s) moved.
> ls
b.txt
𥳠Neat!
But there's one small annoyance now... Creating the aliases isn't always as simple as that, and even then, why talk about Node just to not use it?
I'm sure you can see the positive impact this will have on your terminal sessions for a start.
Using Node for aliases instead of Batch
But what about using Node for aliases? I mean, it's in the name of this post!
Well, funny you should ask, because that's exactly what we're going to do!
The first alias we're going to create is our own custom alias function. This'll work exactly like how NPM's commands work: buy placing our generic alias.cmd
file (which execute our js files) in our PATHed alias folder!
Here's our alias.cmd
file:
If you look close enough, you'll see it's more or less identical to the .cmd
files of NPM's commands. But you'll also notice that we're calling "%curdir%\**js**\%fileCalled%.js"
. We need a new subfolder within our .aliases
folder called js
. A smart move would be to also npm init
within it so we can access NPM packages without installing them globally! (If you do this though, add "private": true
to the package.json
so you don't accidentally publish it to NPM!)
This makes our file structure look like this:
.aliases
â alias.cmd
â ls.cmd
â mv.cmd
â
ââââjs
â package.json
Finally, cd js
, and echo console.log("hello alias!") >> alias.js
. Now if execute alias, you'll see that node is running the alias.js
file! You can now employ JS to handle all your aliases!
A little something extra...
So it's cool and all to have your own alias.js
file run, but it actually isn't too much use to us... At least not yet... Follow these commands and copy the file underneath, and you'll be amazed!
> cd
C:\Users\User\.aliases\js
> npm install big-kahuna
Copy the alias.js
file from this gist:
The file assumes that you're using VSCode as your editor. You can set it by changing line 11. Note that it does open up the alias folder!
Wrapping it up ð¯
Awesome! We now have alias files that are persistent and have (arguably) more functionality than they're normal CMD counterparts! Anything that you can do in NodeJS is possible through this method!
Of course, this is somewhat language-extensible by adding calling different programs instead of node
. For example, you can achieve the same result with python
, rust
, dart
, you name it! If the language can execute files from the command line, you can execute them as an alias!
Enjoy! ðð
Top comments (0)