Skip to content

Instantly share code, notes, and snippets.

@erichiller
Last active March 10, 2024 11:39
Show Gist options
  • Save erichiller/ee1ad352f8a2dd97a1ed606faa43f87f to your computer and use it in GitHub Desktop.
Save erichiller/ee1ad352f8a2dd97a1ed606faa43f87f to your computer and use it in GitHub Desktop.
powershell profile for cmder - revised to load module information from json file via jq.exe
# Stored here:
# https://gist.github.com/erichiller/ee1ad352f8a2dd97a1ed606faa43f87f
# Set the cursor to be at the end of the line when searching history. ie. ^ Up arrow
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
# Change character prefix for multiline commands
Set-PSReadLineOption -ContinuationPrompt ' '
# see possible options under "Tab Complete" here:
# https://github.com/lzybkr/PSReadLine/blob/master/docs/about_PSReadLine.help.txt
# Note: Ctrl + Space already performs MenuComplete
# TabCompleteNext , Complete , MenuComplete
Set-PSReadLineKeyHandler -Key Tab -Function Complete
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
# These keys use alt+arrow which requires `$env:PSREADLINE_VTINPUT=1` to be set BEFORE starting PowerShell
# see issue <https://github.com/PowerShell/PSReadLine/issues/558>
# Set-PSReadLineKeyHandler -Chord "Alt,LeftArrow" -Function BackwardWord
Set-PSReadLineKeyHandler -Chord Alt+LeftArrow -Function BackwardWord
Set-PSReadLineKeyHandler -Chord Ctrl+LeftArrow -Function BackwardWord
# Set-PSReadLineKeyHandler -Chord "Alt,RightArrow" -Function ForwardWord
Set-PSReadLineKeyHandler -Chord Alt+RightArrow -Function ForwardWord
Set-PSReadLineKeyHandler -Chord Ctrl+RightArrow -Function ForwardWord
Set-PSReadLineKeyHandler -Chord Ctrl+Backspace -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Chord Ctrl+Delete -Function DeleteWord
# Modules
Import-Module posh-git
Import-Module DockerCompletion
# Completion
## dotnet cli tool
try {
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
} catch {
Write-Error "Failed to create ArgumentCompleter for dotnet";
}
## dotnet-suggest
. (Join-Path ( Split-Path $PROFILE -Parent ) "dotnet-suggest-shim.ps1")
# EDH Utility Functions
function Convert-UnixTimeToDate {
[OutputType([System.DateTime])]
param(
[Parameter(ParameterSetName = 'Ticks')]
[long]$Ticks,
[Parameter(ParameterSetName = 'Milliseconds')]
[long]$Milliseconds,
[Parameter()]
[switch]$ISO8601
)
$dateTime = $null;
if ( $Ticks -gt 0 ) {
Write-Debug "Ticks=$Ticks"
$dateTime = [System.DateTime]::MinValue.AddTicks($Ticks).AddYears(1970 - 1).ToLocalTime();
}
if ( $Milliseconds -gt 0 ) {
Write-Debug "Milliseconds=$Milliseconds"
$dateTime = [System.DateTime]::MinValue.AddMilliseconds($Milliseconds).AddYears(1970 - 1).ToLocalTime();
}
if ( $ISO8601 ) {
Write-Host $dateTime.ToString("o");
}
if ( $null -eq $dateTime ) {
Write-Error 'No UnixTime data was provided. Please input UnixTime.'
}
return $dateTime;
}
<#
.DESCRIPTION
Wrapper for linux ls to colorize output
#>
function ls {
param(
[string[]]
[Parameter(Position = 0, ValueFromRemainingArguments)]
$CommandArgs
);
/usr/bin/ls --color=auto @CommandArgs
}
# $DebugPreference='Continue'
# Load variables from .edh.bashrc
$bashRcFile = "/home/eric/.edh.bashrc";
Get-Content $bashRcFile `
| Select-String -Pattern "^export\s+(?<Name>\w+)=`"?(?<value>[^`"]*)`"?$" `
| ForEach-Object {
Write-Debug "Setting $($_.Matches.Captures.Groups["Name"].Value) to $($_.Matches.Captures.Groups["value"].Value)";
[Environment]::SetEnvironmentVariable( $_.Matches.Captures.Groups["Name"].Value, $_.Matches.Captures.Groups["value"].Value )
}
# Enable native command errors
# only run once
# https://learn.microsoft.com/en-us/powershell/scripting/learn/experimental-features#psnativecommanderroractionpreference
# Enable-ExperimentalFeature PSNativeCommandErrorActionPreference
$PSStyle.FileInfo.Directory = $PSStyle.Foreground.Blue + $PSStyle.Bold;
$PSStyle.FileInfo.Executable = $PSStyle.Foreground.Green + $PSStyle.Bold;
$PSStyle.FileInfo.SymbolicLink = $PSStyle.Foreground.BrightCyan + $PSStyle.Bold;
$PSStyle.FileInfo.Extension[".cs"] = $PSStyle.Foreground.BrightYellow;
$PSStyle.FileInfo.Extension[".ps1"] = $PSStyle.Foreground.BrightYellow;
$PSStyle.FileInfo.Extension[".csproj"] = $PSStyle.Foreground.Yellow;
$PSStyle.FileInfo.Extension[".sln"] = $PSStyle.Foreground.Yellow;
$PSStyle.FileInfo.Extension[".json"] = $PSStyle.Foreground.Yellow;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment