Permanent alias on Windows

If you develop C++ programs on Windows, you might have felt the need to invoke the compiler from the command prompt itself. Its pretty easy to do so; just append the path of the compiler to the PATH environment variable.
However, if you decide to invoke it with some compiler/linker options, you’ll need to do some extra work.

Unlike Linux, it is a bit tricky to create permanent aliases on Windows as it requires editing the registry.

Before discussing on how to do it on Windows, let me first illustrate how it is done on Linux (Ubuntu specifically):
Suppose you want to set c+ as an alias to c++ -Wall -Wextra, simply create a file in your home folder named .bash_aliases and write the following in it:
alias c+ = 'c++ -Wall -Wextra'
Instead of writing the compiler options -Wall -Wextra directly, I prefer writing them in a separate file and then include them by specifying its path via the @file option of the GCC compiler, so that I don’t have to write them again in my IDE’s settings.

So, .bash_aliases now contains the following:
alias c+ = 'c++ @/path/to/cpp-options.txt'
where cpp-options.txt contains:
-Wall -Wextra

Now you can invoke the compiler by typing the following in a cmd shell:

c+ /path/to/source.cpp

In Windows, the closest thing to alias is doskey.

Just like in the Linux case, we have to somehow instruct cmd to launch the doskey command every time it starts.
To do this, we need to create 2 files:

  • cmd_aliases.txt, which will contain all the aliases:
    c+ = c++ @/path/to/cpp-options.txt $*
    view raw cmd_aliases.txt hosted with ❤ by GitHub

    Note: The $* option is to allow the use of other command-line options along with c+ command. Without it, you won’t be able to specify the source-code and the output executable path when invoking the compiler with c+.

  • cmd_autorun.cmd, which loads all those aliases from the above file everytime cmd is invoked:
    @echo off
    doskey /macrofile=%USERPROFILE%\Documents\cmd_aliases.txt
    REM The `/macrofile` parameter of the `doskey` command is used to load the aliases from an external file.
    view raw cmd_autorun.cmd hosted with ❤ by GitHub

[I am assuming these 2 files are kept in your My Documents folder, although you can keep them in any other folder you like.]

Now we need to specify the location of this script in the registry so that it launches automatically along with cmd.
To do this, execute the following after pasting it to a text file and saving it with .reg extension:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor] "Autorun"="%USERPROFILE%/Documents/cmd_autorun.cmd"

For completeness, here are all the files which I use myself:

-Wall -Wextra -Wfatal-errors -Wunreachable-code -Wfloat-equal -Winline -Wshadow -Wredundant-decls -Wcast-qual -Wcast-align -Wlogical-op -pedantic
view raw cpp-options.txt hosted with ❤ by GitHub

python2 = C:\Dev\Python27\python.exe $*
python3 = C:\Dev\Python34\python.exe $*
c+ = c++ @D:\anmol\coding\c++\cpp-options.txt $*
view raw cmd_aliases.txt hosted with ❤ by GitHub

@echo off
doskey /macrofile=%USERPROFILE%\Documents\cmd_aliases.txt
view raw cmd_autorun.cmd hosted with ❤ by GitHub

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor] "Autorun"="%USERPROFILE%/Documents/cmd_autorun.cmd"


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s