Scripts and Aliases for Terminal / Shell

2025-10-04

When I find myself repeating a command I always try to Alias it in my terminal. When you got enough commands it starts to become your own little language, similar to navigation in Vim. The idea is not only to remove the need to remember every command but also to be able to do more with less typing. Currently I am using the Windows Terminal so all my Aliases end up here: $HOME\Documents\PowerShell\Profile.ps1. Here is a collection of the aliases I use day-to-day.

â„šī¸ These examples are in PowerShell, but the same principle applies in any shell

Repository Navigation

When working without a monorepo you often find yourself working in multiple directories (especially with microservice architecture). Since I use the terminal for all Git handling, navigating to the correct repository is one of my most common operations and therefore one of the most time saving alias. The alias is simple, a word or acronym that navigates to the repository. This is of course not limited to repositories - any directory you find yourself often navigating to benefits from an alias:

Function navigate_order { cd "C:\Users\User\Projects\OrderService" }
Function navigate_payment { cd "C:\Users\User\Projects\PaymentService" }
Function navigate_dispatch { cd "C:\Users\User\Projects\DispatchService" }
New-Alias order navigate_order
New-Alias payment navigate_payment
New-Alias dispatch navigate_dispatch

Opening Current Directory

Sometimes you need to get out of the terminal and back to explorer. I do this by pressing o, which will open the current directory (ii is short for Invoke-Item):

Function open_current_directory { ii . }
New-Alias o open_current_directory

Opening Solutions

What's something you do a lot in these directories? Open the project of course. This we want aliased as well (this example uses Visual Studio - those using Visual Studio Code already have the code . shorthand):

Function open_solution { OpenSolution.ps1 }
New-Alias os open_solution

OpenSolution.ps1:

$currentPath = Get-Location
$slnFiles = Get-ChildItem -Path $currentPath -Include *.sln, *.slnx -Recurse -File

if ($slnFiles.Count -eq 0) {
    Write-Host "No solution (.sln or .slnx) files found in the current repository." -ForegroundColor Red
    exit 1
}
elseif ($slnFiles.Count -gt 1) {
    Write-Host "Multiple solution files found:" -ForegroundColor Yellow
    $slnFiles | ForEach-Object { Write-Host "  $($_.FullName)" -ForegroundColor Yellow }
    Write-Host "Please specify which solution file to open." -ForegroundColor Yellow
    exit 1
}
else {
    $slnFile = $slnFiles[0]
    Write-Host "Opening solution file: $($slnFile.FullName)" -ForegroundColor Green
    
    try {
        Start-Process -FilePath $slnFile.FullName
        Write-Host "Solution file opened successfully." -ForegroundColor Green
    }
    catch {
        Write-Host "Error opening solution file: $($_.Exception.Message)" -ForegroundColor Red
        exit 1
    }
}

Opening the order service in Visual Studio then becomes:

order <Enter>
o <Enter>

Simple and quick.

Kubernetes

If you are a kubectl user you often find yourself drowning in the various commands. Aliasing these is will make life easier.

As an example you can get the pods by simply typing pods:

Function get_pods { kubectl get pods --all-namespaces }
New-Alias pods get_pods

Another example is navigating environments, a common operation in any production system:

Function set_dev { kubectl config use-context project-dev-k8s }
Function set_test { kubectl config use-context project-test-k8s }
Function set_staging { kubectl config use-context project-staging-k8s }
Function set_pt { kubectl config use-context project-pt-k8s }
Function set_prod { kubectl config use-context project-production-k8s }

New-Alias dev set_dev
New-Alias test set_test
New-Alias staging set_staging
New-Alias pt set_pt
New-Alias prod set_prod

Now all you do is type staging to go the Staging environment, or prod to go to Production - follow by pods to list the pods (or any other command you've aliased).

I once worked in a company that had some services in Azure Kubernetes and some in an on-prem OpenShift cluster. This required some trickery to get kubectl to navigate correctly, but once it worked it certainly beat having to manually type the commands.

For a while I used k9s to get an overview of the clusters. Since I only used it to get an overview I always made sure to open it on read-only mode. Aliasing can ensure you never forget it:

Function k9sreadonly { k9s --readonly }
New-Alias k k9sreadonly

Git Commands

Some common Git commands from my profile:

Function gitalias_gs { git status }
Function gitalias_pull_main { git pull origin main }
Function gitalias_gitclean { git clean -d -x -f -e .env -e **/PreRelease/** -e **/*.vs }
New-Alias gs gitalias_gs
New-Alias gpom gitalias_pull_main
New-Alias gitc gitalias_gitclean

Note the specific options in the Git clean alias. Since these are your own aliases and should correspond to what you usually do don't be afraid to include arguments. For example, for a while I ran git clean a lot, but wanted to avoid removing some specific files that were being downloading during run-time. Adding the files in the exclude option made it easy to clean and never make a mistake.

Navigation Directories

Writing cd .. can be shorted to cd.. which can be shorted to ... But why stop there? This Alias adds backwards navigation depending on how many dots you type. .. = go up one level, ... = go up two levels, etc.

I'm sure there is a clean way to optimize this, but since I rarely go up more than three levels I've avoided the premature optimization:

Function navigate_back { cd .. }
Function navigate_back2 { cd ../.. }
Function navigate_back3 { cd ../../.. }
New-Alias cd.. navigate_back
New-Alias .. navigate_back
New-Alias ... navigate_back2
New-Alias .... navigate_back3

Summary

This was a short post about some of the aliases I use every day. There are plenty of other nifty tricks you can add to your aliases that are documented on the internet. My biggest advice is to not only use these ones but actually create your own specifically targeting your most common operations.


Back to posts.