Skip to content

Instantly share code, notes, and snippets.

@diogotito
Created June 27, 2024 18:14
Show Gist options
  • Save diogotito/199448685b8d4bf1064d644628fb0feb to your computer and use it in GitHub Desktop.
Save diogotito/199448685b8d4bf1064d644628fb0feb to your computer and use it in GitHub Desktop.
PowerShell core (pwsh) $profile with some manual profiling of modules and initialisation steps
# Write-Host -ForegroundColor Green 'Measuring...'
$stopWatch = [System.Diagnostics.Stopwatch]::new()
$stopWatch.Start()
[System.Collections.ArrayList] $stops = @()
[double] $last = 0.0
function stop($descr) {
$t = $stopWatch.Elapsed.TotalMilliseconds
$d = $t - $last
[void] $stops.Add([PSCustomObject]@{
Accumulated = "`e[36m$t`e[0m"
Time = $(if ($d -gt 50) { "`e[35m{0:f}" -f $d } else { $d })
Description = $(if ($d -gt 50) { "`e[35m$descr" } else { $descr })
})
$script:last = $t
}
function Measure-Startup([switch] $silent = $false) {
$sw = [System.Diagnostics.Stopwatch]::new()
$sw.Start()
pwsh -c '$stops | Format-Table @{n="Accumulated";e={$_.Accumulated};a="r"},@{n="Time";e={$_.Time};a="r"},Description'
$sw.Stop()
$vanilla_pwsh = (Measure-Command { pwsh -NoProfile -c { 42 } }).TotalMilliseconds
if ( -not $silent ) {
Write-Host "Vanilla PowerShell 7.4: $vanilla_pwsh ms"
Write-Host "Total command duration: $($sw.Elapsed.TotalMilliseconds) ms"
Write-Host
Write-Host -ForegroundColor Green "Startup bloat:"
Write-Host ($sw.Elapsed.TotalMilliseconds - $vanilla_pwsh) ms
Write-Host
}
}
stop "(⏱ stopwatch setup)"
# UTF-8
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
$PSDefaultParameterValues["Out-File:Encoding"] = "utf8NoBOM"
$PSDefaultParameterValues["Set-Content:Encoding"] = "utf8NoBOM"
$PSDefaultParameterValues["Add-Content:Encoding"] = "utf8NoBOM"
$PSDefaultParameterValues["Export-CSV:Encoding"] = "utf8NoBOM"
$PSReadLineOptions = @{
EditMode = "Emacs"
HistoryNoDuplicates = $true
HistorySearchCursorMovesToEnd = $true
Colors = @{
"Command" = "#8181f7"
}
}
stop "PSReadLine 0"
Set-PSReadLineOption @PSReadLineOptions
stop "PSReadLine 1 (EMACS KEYS!!)" # 120 ms
$isWT = @( $p = ps -Id $PID; while ($p) { $p; $p = $p.Parent } ).ProcessName -contains 'WindowsTerminal'
# $isWT = $( $p = ps -Id $PID; while ($p) { if ($p.ProcessName -eq 'WindowsTerminal') { break $true }; $p = $p.Parent } $false )
stop "PSReadLine 2 (detect wt.exe)" # 36 ms
if ($isWT) {
Set-PSReadLineOption -PredictionSource HistoryAndPlugin -PredictionViewStyle ListView
}
Set-PSReadLineKeyHandler -Key Ctrl+z -Function Undo
Set-PSReadLineKeyHandler -Key Ctrl+/ -Function Undo
Set-PSReadLineKeyHandler -Key Ctrl+v -Function Paste
Set-PSReadLineKeyHandler -Key Ctrl+V -Function Paste
Set-PSReadLineKeyHandler -Key Ctrl+j -Function AcceptLine
Set-PSReadLineKeyHandler -Key Ctrl+Shift+L -ScriptBlock { Clear-Host; [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() }
Set-PSReadLineKeyHandler -Key Shift+Spacebar -ScriptBlock { [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') }
Set-PSReadLineKeyHandler -Key Shift+Insert -Function Paste
Set-PSReadLineKeyHandler -Key Ctrl+LeftArrow -Function BackwardWord
Set-PSReadLineKeyHandler -Key Ctrl+RightArrow -Function NextWord
Set-PSReadLineKeyHandler -Key Ctrl+Shift+LeftArrow -Function SelectBackwardWord
Set-PSReadLineKeyHandler -Key Ctrl+Shift+RightArrow -Function SelectForwardWord
Set-PSReadLineKeyHandler -Key Ctrl+Backspace -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Key Ctrl+Delete -Function DeleteWord
# Fancy ZSH/Fish-like tab completion
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
# Make Ctrl+x Cut if we are selecting stuff
# Do Emacs' C-x C-x otherwise
Set-PSReadLineKeyHandler -Key Ctrl+x -ScriptBlock {
param($key, $arg)
# Get selection length in $selectionLength
$selectionStart = $null
$selectionLength = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref] $selectionStart, [ref] $selectionLength);
if ($selectionLength -gt 0) {
[Microsoft.PowerShell.PSConsoleReadLine]::Cut($key, $arg)
}
else {
# I'm not waiting for the second C-x in C-x C-x to trigger Emacs' behaviour
[Microsoft.PowerShell.PSConsoleReadLine]::ExchangePointAndMark($key, $arg)
}
}
stop "PSReadLine 3 (custom keybindings)"
function rc() {
vim --cmd 'set fileformats=dos' $profile
. $profile
}
Set-Alias c Clear-Host
# Git aliases
Set-Alias g git
Set-Alias lg lazygit
"ca", "cav", "co", "cl", "d", "l", "lga", "loga", "p", "ru", "s", "st" | % {
"function g$_() { g $_ `$args }"
} | iex
function Get-GitAliases() {
(git config --list) -match '^alias' | ForEach-Object {
$parts = [regex]::Match($_, '^alias\.([^=]*)=((\S*)(\s?.*))').Groups.Value
[pscustomobject] @{
Alias = $parts[1];
Command = $(
if ($parts[2] -notlike '!*') {
"`e[32mgit`e[34m $($parts[3])`e[0m$($parts[4])"
}
else {
"`e[35m$($parts[2].Substring(1))`e[0m"
}
)
}
} | Sort-Object -Property Alias
}
Set-Alias ggal Get-GitAliases
stop "Git aliases setup" # 40 ms
# Oh-My-Posh
function theme() {
param([ArgumentCompleter({
param($_commandName, $_parameterName, $wordToComplete, $_commandAst, $_fakeBoundParameters)
Get-ChildItem $Env:POSH_THEMES_PATH -Filter '*.omp.json'
| Where-Object Name -like "$wordToComplete*.omp.json"
| ForEach-Object { $_.Name -replace '\.omp\.json', '' }
})] $theme)
Import-Module posh-git
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/$theme.omp.json" | Invoke-Expression
}
function gr() { theme gruvbox }
function PimpMy-ls() {
Remove-Alias ls -ErrorAction Ignore
function global:ls() {
eza.exe -F --long --group-directories-first --git --icons --all @args
}
}
# Starship
# Invoke-Expression (& 'C:\Users\diogotito\scoop\shims\starship.exe' init powershell --print-full-init | Out-String)
# stop Starship # 110 ms
# z
# Import-Module ZLocation
# stop ZLocation # 400 ms
# Lazy-load ZLocation z
function z() {
Write-Host -NoNewline "✨ Loading ZLocation's z`r"
Import-Module ZLocation
Remove-Item Function:\z
PimpMy-ls
Write-Host -NoNewline " `r"
z @args
}
# cd-extras - https://github.com/nickcox/cd-extras
# Import-Module cd-extras
# stop cd-extras # 185 ms
# theme gruvbox
# stop oh-my-posh # 500 ms
# Search winget, Scoop and Chocolatey simultaneously in Windows Terminal
function pkg([string] $name) {
function Build-SearchCmd([string] $cmd) {
"C:\Windows\system32\cmd.exe", "/C"
@("ECHO ^> $cmd", "$cmd", "PAUSE") -join ' & '
}
wt split-pane --vertical --size 0.5 (Build-SearchCmd "winget search $name") `; `
split-pane --horizontal --size 0.66 (Build-SearchCmd "scoop-search $name") `; `
split-pane --horizontal --size 0.5 (Build-SearchCmd "choco search $name") `; `
focus-pane --target 0
}
function Which() {
Get-Command @args | select CommandType, Name, Source
}
# Aliases for PowerShell commands I haven't fully memorized yet
Set-Alias grep Select-String
Set-Alias eval Invoke-Expression
Set-Alias time Measure-Command
Set-Alias exa eza
function CatN() { $input | Select-String "^" | Select-Object -Property LineNumber, Line }
function ff() { ffmpeg -hide_banner $args }
# Import-Module 'C:\git\NARC\vcpkg\scripts\posh-vcpkg'
#
# WORKFLOW: fpt
#
Set-Alias ca cargo
function f() {
function wow($n, $msg) { Write-Host -NoNewline "[$n/3] ✨ $(($msg + "...").PadRight(40))`r" }
Set-Location C:\git\fpt
stop ... ; wow 1 "Loading oh-my-posh"; theme gruvbox
stop oh-my-posh; wow 2 "Loading ZLocation "; Import-Module ZLocation
stop ZLocation ; wow 3 "Loading cd-extras "; Import-Module cd-extras
stop cd-extras ; ; PimpMy-ls
}
if ($isWT -and $pwd -like 'C:\git\fpt*') {
stop ...
f
stop "✨ fancy setup for fpt ✨ oh-my-posh + ZLocation + cd-extras ✨"
}
$stopWatch.Stop()
stop "(⏱ total startup time)"
Set-Alias msu Measure-Startup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment