Creating custom terminal commands
Creating custom terminal commands can change having to type something long like this:
py -3.10 "C:\Users\chris\Documents\programming\refactor-todo-list\create_task.py"
to something like this:
todo
This will work no matter which directory the terminal is in and can temporarily change directories and/or activate a virtual environment if needed.
Some programming languages have their own tools for creating custom terminal commands (links to a few are listed below), but sometimes creating the command in one of the other ways on this page is easier overall.
Mac and Linux
This guide seems well-written (no need to rewrite the wheel).
Windows
I recommend checking out Windows Terminal if you’re choosing a terminal app.
temporary commands
If you already have an executable file, a super quick way to create a command that only lasts until you close the terminal is to run $env:Path += ';C:\path\to\my\executable\file.exe'
in PowerShell.
permanent commands
- In PowerShell, enter
notepad $profile
. This should open a PowerShell file (the file extension should be.ps1
). - In this file, you can create custom terminal commands by defining PowerShell functions. For example,
function hi {
Write-Host "hello"
}
This creates a hi
command that responds with hello
when used.
- Save the file.
- Restart your terminal.
- Try your new command.
Here are more examples:
function up {
cd ..
}
# This command kind of imitates Linux's command for creating new files.
function touch {
new-item "$args" -ItemType File
}
# This function runs a program.
function matrix {
& "C:\Users\chris\Documents\programming\the_matrix\x64\Debug\the_matrix.exe"
}
# You can run any type of executable file this way, not just .exe files.
function psql {
& "C:/ProgramData/Microsoft/Windows/Start Menu/Programs/PostgreSQL 15/SQL Shell (psql).lnk"
}
# You can shorten commonly used existing commands.
function g {
git $args
}
# This runs a Python program. The $args at the end sends any inputs for the command from
# the terminal to the program. If all the inputs should be in one string, you can put
# double quotes around it: "$args". Note that any inputs with commas on the command-line
# will still need quotes around them for all the input to be in one string.
function rand {
py C:/Users/chris/Documents/programming/rand/main.py $args
}
# Some programs need to temporarily change the directory and/or activate a virtual
# environment while running.
function todo {
$originalPath = pwd
cd C:/Users/chris/Documents/programming/refactor-todo-list
try {
venv/Scripts/Activate.ps1
try {
py create_task.py "$args"
} finally {
deactivate
}
} finally {
cd $originalPath
}
}
# Here's how you can customize what your terminal prompts look like.
function prompt {
# https://hodgkins.io/ultimate-powershell-prompt-and-git-setup
$realLASTEXITCODE = $LASTEXITCODE
Write-Host $($(Get-Location) -replace "\\", "/") -ForegroundColor Blue
$global:LASTEXITCODE = $realLASTEXITCODE
return "> "
}
# This command shows or searches your command history across sessions (unlike the built-in
# `h` and `r` commands). This was written by Adam Wemlinger.
function hist {
# https://superuser.com/questions/1195895/view-full-history-for-powershell-not-just-current-session
$find = $args;
Write-Host "Finding in full history using {`$_ -like `"*$find*`"}";
Get-Content (Get-PSReadlineOption).HistorySavePath | ? {$_ -like "*$find*"} | Get-Unique | more
}
# Activate a Python virtual environment named "venv" by simply entering "venv". For
# environments with something after venv like "venv311", enter "venv 311".
function venv {
if ($args) {
Invoke-Expression "venv$args/Scripts/Activate.ps1"
} else {
venv/Scripts/Activate.ps1
}
}
a folder of executables
If you don’t want to add all your commands to a file like above, you can create a folder, add it to PATH (see Edit PATH in Windows), and add executable files to that folder. Many people do this. They usually name the folder bin
(short for binaries, a synonym to executable files) and put it in their home folder (in my case, that’s C:/Users/chris
). On some operating systems, it can also be a good idea to start all of your commands with a comma.
The files can truly be any type of executable file, such as .exe
, or .py
if you’re using Python, or .bat
or .cmd
if you want to use Batch scripts, or .ps1
if you would prefer to use PowerShell commands, etc. After adding these file(s) to the folder, restart your terminal and enter the file’s name without the extension to run the command. Personally, I use a mixture of binaries in a C:/Users/chris/bin
folder and the $profile
file.
more about PowerShell
- customizing terminal prompts in Windows
- Microsoft’s docs
- PowerShell Gotchas
- the
alias
command lists all aliases - although many PowerShell guides show commands with uppercase letters, like
Get-Location
, PowerShell is not case-sensitive, so you can useget-location
(or its aliaspwd
)
how to receive command line arguments
Every widely-used general-purpose programming language has a way to receive command line inputs. Here are guides for a few:
- C++ command line arguments
- Python’s
argparse
library - Go’s
flag
library and the spf13/cobra framework that builds on top of it - Command Line Interface Guidelines by Prasad et al. includes a list of command-line argument parsing libraries