Skip to content

Instantly share code, notes, and snippets.

@efrec
Created May 13, 2023 01:03
Show Gist options
  • Save efrec/48a05c5ed9bf9f2591f41410526b9a41 to your computer and use it in GitHub Desktop.
Save efrec/48a05c5ed9bf9f2591f41410526b9a41 to your computer and use it in GitHub Desktop.
## Command history
# Improve on PSReadLine's history heuristic without having to re-implement from scratch.
$commandHistoryHandler = @{
HistoryNoDuplicates = $false
HistorySaveStyle = [Microsoft.PowerShell.HistorySaveStyle]::SaveIncrementally
AddToHistoryHandler = {
param([string] $line)
<# spammy #> if ($line -in 'exit', 'ls', 'pwd', 'cls', 'clear' ) { return $false }
<# buried #> if ($line -match '\A[ ]{2}\S') { return $false }
<# assist #> if ($line -match '(?<=^|\b)(?<!\$)(help|get-help|man)(?=\b|$)(?![-:]| *=)') {
$sensitive = -not [Microsoft.PowerShell.PSConsoleReadLine]::GetDefaultAddToHistoryOption($line)
return $sensitive ? $false : [Microsoft.PowerShell.AddToHistoryOption]::MemoryOnly
}
<# secret #> if ($line -match 'password|asplaintext|securestring|from-secure|to-secure|key|token') {
return [Microsoft.PowerShell.PSConsoleReadLine]::GetDefaultAddToHistoryOption($line)
}
<# normal #> return [Microsoft.PowerShell.AddToHistoryOption]::MemoryAndFile
}
}
Set-PSReadLineOption @commandHistoryHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment