|
<# |
|
.SYNOPSIS |
|
My custom PowerShell user profile. |
|
|
|
.DESCRIPTION |
|
The first time setting up this profile on a new computer, modify developer options to |
|
allow scripts to run and run the following: |
|
|
|
. "Microsoft.PowerShell_profile.ps1" |
|
Initialize-Profile |
|
|
|
Place this profile at `C:\Users\<you>\Documents\PowerShell`, or wherever $PROFILE points to. |
|
#> |
|
|
|
function Initialize-Profile { |
|
<#.SYNOPSIS |
|
Setup for my PowerShell profile. |
|
#> |
|
|
|
# Required to use Set-PSEnv in the ENVIRONMENT section |
|
Install-Module -Name Set-PSEnv |
|
|
|
# See `Get-ExperimentalFeature` for details. Once enabled, they stay enabled. |
|
Enable-ExperimentalFeature PSCommandNotFoundSuggestion |
|
Enable-ExperimentalFeature PSLoadAssemblyFromNativeCode |
|
Enable-ExperimentalFeature PSNativeCommandErrorActionPreference |
|
Enable-ExperimentalFeature PSSubsystemPluginModel # For PSREADLINE section |
|
|
|
# For up-to-date help and useful keybinds (F1, Alt+H, etc.) |
|
Update-Help -Force # Need force because some issues w/ PSReadLine |
|
} |
|
|
|
# * -------------------------------------------------------------------------------- * # |
|
# * ENVIRONMENT |
|
|
|
function Set-Env { |
|
<# |
|
.SYNOPSIS |
|
Load environment variables from `.env`, activate virtual environments. |
|
#> |
|
|
|
# If there is an `.env` file in the PWD, load environment variables from it |
|
Set-PsEnv |
|
# Look for a virtual environment |
|
$Activate = '.venv/scripts/activate.ps1' |
|
if (Test-Path $Activate) { |
|
# If there is a Python virtual environment in the PWD, activate it |
|
& $Activate |
|
} |
|
} |
|
|
|
Set-Env |
|
|
|
# * -------------------------------------------------------------------------------- * # |
|
# * ALIASES AND SHORTHAND COMMANDS |
|
|
|
# New-Alias -Name 'codei' -Value 'code-insiders' |
|
|
|
# Remove-Alias -Force 'gc' # Read-only alias for Get-Content |
|
# function gc { |
|
# git commit @args |
|
# } |
|
|
|
# Remove-Alias -Force 'gps' # Read-only alias for Get-Unique |
|
# function gps { |
|
# git push @args |
|
# try { dvc push } catch [System.Management.Automation.CommandNotFoundException] {} |
|
# } |
|
|
|
# function gpl { |
|
# git pull @args |
|
# try { dvc pull } catch [System.Management.Automation.CommandNotFoundException] {} |
|
# } |
|
|
|
# * -------------------------------------------------------------------------------- * # |
|
# * DEFAULT ENVIRONMENT VARIABLES |
|
|
|
# $MY_PROFILE = $PROFILE |
|
# $Env:PIP_DISABLE_PIP_VERSION_CHECK = 1 |
|
# $Env:POWERSHELL_TELEMETRY_OPTOUT = 'true' |
|
# $Env:PYDEVD_DISABLE_FILE_VALIDATION = 1 |
|
# $Env:PYTHONWARNDEFAULTENCODING = 1 |
|
|
|
# * -------------------------------------------------------------------------------- * # |
|
# * |
|
|
|
# Fix leaky UTF-8 encoding settings, now PowerShell pipes will be UTF-8. Note that |
|
# fixing it from Control Panel and system-wide has buggy downsides. |
|
# See: https://github.com/PowerShell/PowerShell/issues/7233#issuecomment-640243647 |
|
[console]::InputEncoding = [console]::OutputEncoding = [System.Text.UTF8Encoding]::new() |
|
|
|
# $ErrorActionPreference = 'Stop' |
|
# $ErrorView = 'NormalView' |
|
# $ErrorView | Out-Null # https://github.com/PowerShell/PSScriptAnalyzer/issues/1749 |
|
|
|
# * -------------------------------------------------------------------------------- * # |
|
# * PROMPT |
|
|
|
function Edit-Location { |
|
<#.SYNOPSIS |
|
Return the current directory, showing the root drive followed by an ellipsis and |
|
only as many folders back from the current folder (inclusive) as desired. |
|
#> |
|
|
|
Param( |
|
# The number of folders back from the current folder (inclusive) to keep. |
|
[int][Parameter(Mandatory)]$count |
|
) |
|
|
|
$lookbehind = '(?<=\\)' # Look behind for a backslash. |
|
$match = '[\s\S]+' # Match anything at least one character long. |
|
# Look ahead for a backslash, |
|
# followed by anything (at least one char.) except a backslash, |
|
# for $count occurrences. |
|
$lookahead = "(?=(\\[^\\]+){$count})" |
|
|
|
# This replaces the match with an ellipsis. The lookbehind ensures the backslash is |
|
# kept before the folders replaced by an ellipsis, while the lookahead ensures the |
|
# desired $count of folders closest to the current folder (inclusive) are kept. |
|
return $((Get-Location) -replace "$lookbehind$match$lookahead", '...') |
|
} |
|
|
|
# function prompt { |
|
# <#.SYNOPSIS |
|
# This function is called by PowerShell to display the prompt. |
|
# #> |
|
|
|
# $(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + |
|
# 'PS ' + $(Edit-Location 3) + $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> ' |
|
# } |
|
|
|
# * -------------------------------------------------------------------------------- * # |
|
# * PSREADLINE |
|
# * |
|
# * Press F2 to toggle between prediction view styles (PredictionViewStyle). |
|
|
|
Set-PSReadLineOption -PredictionSource HistoryAndPlugin |
|
Set-PSReadLineKeyHandler -Chord Tab -Function AcceptNextSuggestionWord |
|
Set-PSReadLineKeyHandler -Chord Shift+Tab -Function TabCompleteNext # Rebind this |
|
|
|
# To avoid VSCode keybind conflicts. |
|
Set-PSReadLineKeyHandler -Chord Shift+F1 -Function ShowCommandHelp # Also on F1 |
|
Set-PSReadLineKeyHandler -Chord F4 -Function CharacterSearch # Also on F3 |
|
Set-PSReadLineKeyHandler -Chord Shift+F4 -Function CharacterSearchBackward # Also on Shift+F3 |