EverythingPython

Alias Creation in Windows Using Python

One thing I’ve missed in Windows post my switch from Linux is the ability to create aliases for commands.

In Linux, you can create an alias for a command by adding the following line to your .bashrc or .bash_profile file -

1alias ll='ls -l'

This would allow you to use ll instead of ls -l in your terminal.

In Windows, you can create a similar alias by creating a batch file and adding it to your PATH environment variable.

Here’s a simple Python script that can help you create a batch file that can be used as an alias -

 1import os
 2
 3# Creating a folder in the user's home directory to store the aliases
 4
 5home_dir = os.path.expanduser('~')
 6alias_dir = os.path.join(home_dir, 'aliases')
 7os.makedirs(alias_dir, exist_ok=True)
 8
 9# Adding the alias folder to the PATH environment variable in Windows
10
11os.system(f'setx PATH "%PATH%;{alias_dir}"')
12
13def create_alias(alias_name, command):
14    with open(f'{os.path.join(alias_dir,alias_name)}.bat', 'w') as f:
15        f.write(f'@echo off\n{command}')
16
17if __name__ == '__main__':
18    alias_name = input('Enter the alias name: ')
19    command = input('Enter the command: ')
20    create_alias(alias_name, command)
21    print(f'Alias {alias_name} created successfully!')

You can run this script and provide the alias name and the command you want to run when the alias is called.

For example, if you want to create an alias ll for dir, you can run the script and provide ll as the alias name and @dir %* as the command.

This will create a batch file called ll.bat that you can add to your PATH environment variable and use ll as an alias for dir in your terminal.

#Application