Skip to main content

Microsoft.PowerShell_profile.ps1

Created on Sun Feb 09 2025Last updated on Mon Feb 10 2025
# General modules
Import-Module -Name PSReadLine # typeahead predictions and whatnot
Import-Module -Name Terminal-Icons # Icons when listing directories
Import-Module -Name PSFzf # activate using `Ctrl T`, `Ctrl R` and `Alt C`
Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1 # Exposes `refreshenv`
Invoke-Expression (&starship init powershell)

# Advanced completion features (arrow up and down after having started typing)
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

# Autocomplete suggestions
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete # options: [Complete|MenuComplete]

# Autocomplete predictions [HistoryAndPlugin, History, Plugin] requires PS 7.1+
Set-PSReadLineOption -PredictionSource HistoryAndPlugin # accept prediction using right arrow
Set-PSReadLineOption -Colors @{ InlinePrediction = '#888888' }

# accepting suggestions
Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord # Accept one word
Set-PSReadLineKeyHandler -Chord "Ctrl+Spacebar" -Function ForwardChar # Accept full suggestion

############################################################
# Support colors in tools that trigger on commit hooks #
############################################################

$env:FORCE_COLOR = 1

####################################
# Support Linux commands #
####################################

# Unix equivalent(ish) of `host` command.
function host {
param ($IpOrHostname)
$ErrorActionPreference = 'stop'

$Answer = Resolve-DnsName $IpOrHostname
Write-Output $Answer;
}

####################################
# Functions #
####################################

function Remove-LocalGitBranchesExceptMain {
git branch | ForEach-Object {
$_ = $_.Trim()
if ($_ -ne "main" -and $_ -ne "* main") {
git branch -D $_
}
}
}