Skip to content

Instantly share code, notes, and snippets.

@kapsiR
Last active April 24, 2024 13:12
Show Gist options
  • Save kapsiR/4a0c19bb2e7dcc5484dc723777bca94a to your computer and use it in GitHub Desktop.
Save kapsiR/4a0c19bb2e7dcc5484dc723777bca94a to your computer and use it in GitHub Desktop.
PowerShell Profile
function SetupProfile()
{
Import-Module posh-git
$GitPromptSettings.BranchColor.ForegroundColor = 'Black'
$GitPromptSettings.BeforeStatus.ForegroundColor = 'Black'
$GitPromptSettings.AfterStatus.ForegroundColor = 'Black'
$GitPromptSettings.DelimStatus.ForegroundColor = 'Black'
$GitPromptSettings.BranchBehindAndAheadStatusSymbol.ForegroundColor = 'DarkRed'
oh-my-posh.exe init pwsh --config "~/.oh-my-posh.omp.json" | Invoke-Expression
Import-WslCommand "grep", "head", "tail", "less", "man", "du", "hexdump", "dig", "awk", "pandoc", "openssl", "sed", "wc", "jq"
Import-Module Terminal-Icons
# dotnet suggest shell start
# https://github.com/dotnet/command-line-api/blob/main/src/System.CommandLine.Suggest/dotnet-suggest-shim.ps1
if (Get-Command "dotnet-suggest" -errorAction SilentlyContinue)
{
$availableToComplete = (dotnet-suggest list) | Out-String
$availableToCompleteArray = $availableToComplete.Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries)
Register-ArgumentCompleter -Native -CommandName $availableToCompleteArray -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$fullpath = (Get-Command $commandAst.CommandElements[0]).Source
$arguments = $commandAst.Extent.ToString().Replace('"', '\"')
dotnet-suggest get -e $fullpath --position $cursorPosition -- "$arguments" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
else
{
"Unable to provide System.CommandLine tab completion support unless the [dotnet-suggest] tool is first installed."
"See the following for tool installation: https://www.nuget.org/packages/dotnet-suggest"
}
$env:DOTNET_SUGGEST_SCRIPT_VERSION = "1.0.2"
# dotnet suggest script end
# https://docs.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete#powershell
# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
SetupProfile
# Enable posh-git support
# https://ohmyposh.dev/docs/segments/git
function Set-PoshGitStatus {
$global:GitStatus = Get-GitStatus
$env:POSH_GIT_STRING = Write-GitStatus -Status $global:GitStatus
}
New-Alias -Name 'Set-PoshContext' -Value 'Set-PoshGitStatus' -Scope Global -Force
function Get-ChildItemWideFormatted
{
param (
[string[]]$Paths,
[int]$ColumnCount,
[switch]$Force
)
if ($Paths.Length -eq 0) {
$Paths = Get-Location
}
if ($ColumnCount -eq 0) {
$ColumnCount = 5
}
foreach ($Path in $Paths) {
if ($Force) {
Get-ChildItem -Force $Path | Format-Wide -Column $ColumnCount
}
else {
Get-ChildItem $Path | Format-Wide -Column $ColumnCount
}
}
}
function Update-DotNet-Global-Tools
{
foreach ($toolInfoRow in $(dotnet tool list -g | Select-Object -Skip 2))
{
$toolInfo = $toolInfoRow.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
$toolName = $toolInfo[0]
$toolVersion = $toolInfo[1].Trim()
# Fetch version from NuGet
$nugetInfo = (dotnet tool search --take 1 $toolName | Select-Object -Skip 2)
$nugetVersion = $nugetInfo.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)[1].Trim()
if ($nugetVersion -ne $toolVersion)
{
Write-Host "dotnet tool update -g $toolName"
dotnet tool update -g $toolName
}
else
{
Write-Host "Skip $toolName (no updates available)"
}
}
}
Set-Alias -Name ls Get-ChildItemWideFormatted
Set-Alias -Name build-lead D:\Projects\HuH.IsproNG.Web\build-lead.ps1
# Accept first suggested word only
Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord
# https://github.com/PowerShell/PSReadLine/blob/master/PSReadLine/SamplePSReadLineProfile.ps1#L13-L21
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
# Treat lines as sensitive, if first char is space or semicolon
# https://github.com/PowerShell/PSReadLine/issues/2698#issuecomment-1439942616
$defaultHistoryHandler = (Get-PSReadLineOption).AddToHistoryHandler;
Set-PSReadLineOption -AddToHistoryHandler {
param([string]$line)
$defaultHandlerResult = $defaultHistoryHandler.Invoke($line)
if ($defaultHandlerResult -eq "MemoryAndFile")
{
return $line.Length -gt 3 -and $line[0] -ne ' ' -and $line[0] -ne ';'
}
return $defaultHandlerResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment