Background

Over the past year, I've had multiple GitHub repositories for developing pallar, and it was inconvenient to switch between them. All of the projects used Python and Django, and I found it tedious to switch to the correct folder and run the command to start the local Django service for development. So I started to figure out if I could use a single command to streamline the process, and here's what I found.

Steps

1. Create a custom script

Firstly, you need to create a script and name it .custom_commands.sh. Place this file in the root folder, and then you can create multiple functions within it.

$ touch .custom_commands.sh

For example, you could create two functions called service1 and service2, and for each function, the terminal will navigate to the specified folder and execute the Python shell with Django.

.custom_commands.sh
#!/bin/bash

function service1() {
    cd /Users/paul/dev/test1
    python manage.py runserver 127.0.0.1:8000
}

function service2() {
    cd /Users/paul/dev/test2
    python manage.py runserver 127.0.0.1:3000
}

2. Source the script

After creating the script, you need to use the source command to read and execute the script content.

$ source ~/.custom_commands.sh

or you can put this command to the .bashrc or .zshrc file depend on which one you used.

3. Execute it

➜ service1
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2023 - 15:27:58
Django version 3.2.15, using settings 'config.settings.dev'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Conclusion

This blog post shares how to create your own commands on a Mac. It may take some time to become familiar with your own commands, but enjoy the journey!