Making aliases on Windows
Short “How To” about emulating alias
Linux functionality on Windows machine. My default box at work runs Windows OS. Corporate policy. I am creature of habits. Could not change them so drastically, therefore I decided to temper a bit with Windows to provide me at least some basic functionalities I am used to.
Case
Provide Linux OS alias
functionality on Windows. Most Linux distributions use .bashrc
and higher rank files to store info about paths or user aliases. They are ready whenever user logs in. If changed, simple source or bash restart provides newest version.
Personally I do not like to write long commands in bash i.e git log --pretty=oneline --abbrev-commit --graph --branches
(I know git has it’s own system of aliases). I also have bunch of shortcuts I use to navigate or manage files. I prefer to use simple ..
instead of cd ..
or type simple jn
instead of jupyter notebook
etc… Also, I would like to have list of aliases stored permanently in my user files (for future modifications) and to have them accessible in every command line window, when it starts.
Doskey
Windows command line tool has doskey
which allows to create users own macros.
Great! I can put all handy aliases in the batch file. For example:
C:\Users\MIDY\mydoskey.cmd
What do we have here? A command activating and running jupyer in specific environment, with specific python version. A today
command invoking python interpreter to give me date and time at this moment. Shortcuts for too long jupyter commands, same with git. At the end of the script I invoke clean, to start with new prompt.
Two pro-tips:
- No spaces after and before
=
. - Do not name your
.cmd
filedoskey.cmd
. It confuses Windows :)
This way, when I execute mydoskey.cmd
file, I will have handy shortcuts in current command line session. Wait… Just current session? How about brand new cmd window? Do I have to navigate to this file and execute it each time I want my aliases?! Like Raymond Hettinger used to say: There must be a better way. In Linux user aliases are stored in .bashrc
and system provides them when user logs in. How to achieve it in Windows?
Registry
How to make our aliases available automatically, when I start new cmd window? Well, we have got to go deeper and dig in system registry. So, I open Registry Editor (Win+R
and regedit
) and navigate to the Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
Add new REG_EXPAND_SZ
registry entry named Autorun with value pointing to my cmd file with aliases. Entry type is inner registry type for ASCII string. I did it like this:
And voila! Ready. Now whenever new command line starts I can see doskey commands running through the screen (.cmd
file fired up by starting window and subsequent cleaning job) and I can start using them.
Happy coding!