-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
executable file
·133 lines (104 loc) · 4.69 KB
/
Microsoft.PowerShell_profile.ps1
File metadata and controls
executable file
·133 lines (104 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Run `mise install` to install required dependencies (uv, oh-my-posh) then see README for PowerShell modules)
# Default handler excludes lines like /password/ -- Instead use bash pattern of excluding lines with leading space
# https://github.com/PowerShell/PSReadLine/blob/bc485e0208d5dbf44c3d92a1dec9d466c41afc36/PSReadLine/History.cs#L116
Set-PSReadlineOption -AddToHistoryHandler { param ($line) -not $line.StartsWith(' ') }
Set-PSReadlineOption -MaximumHistoryCount 32767
$PSNativeCommandUseErrorActionPreference = $true
$IsStandardLaunch = -not [Console]::IsInputRedirected -and [Environment]::GetCommandLineArgs().Count -eq 1
function .. { cd .. }
function ... { cd ..\.. }
function .... { cd ..\..\.. }
function ..... { cd ..\..\..\.. }
function ...... { cd ..\..\..\..\.. }
function pushtmp {
$tmp = Join-Path "Temp:" "DEL-$(ymd -time)"
New-Item -ItemType Directory -Path $tmp | Out-Null
Push-Location $tmp
}
Set-Alias py python3
Set-Alias python python3
Set-Alias pester Invoke-Pester
# Windows pyenv doesn't shim `python3` ...so that's unsupported
# TODO loop over PY files with '# /// script' and create the functions?
# Can't use Set-Alias because aliases in pwsh don't support arguments!
# see https://web.archive.org/web/20120213013609/http://huddledmasses.org:80/powershell-power-user-tips-bash-style-alias-command/
function gpx { uv run (Join-Path $PSScriptRoot gpx.py) @args }
function stravart { pipx run (Join-Path $PSScriptRoot stravart.py) @args }
function vspy { py (Join-Path $PSScriptRoot vscode_python_interpreter.py) @args }
Set-Alias pipdeptree pytree # pipx complains if it finds `pipdeptree` in the PATH
function ipy {
py -m IPython @args
}
$ENV:PYENV_SHELL = "pwsh"
function wh($ex) {
$cmds = gcm $ex -All -ErrorAction SilentlyContinue
if (-not $cmds) {
Write-Error "Command '$ex' not found"
return
}
foreach ($cmd in $cmds) {
$cmd
if ($cmd.CommandType -ne 'Alias') { continue }
wh $cmd.ResolvedCommand
}
}
function export($s) {
$n, $v = $s -split '=',2
Set-Item "env:$n" $v
Set-Variable -Name $n -Value $v -Scope Global # Ensures that bash syntax $ABC just works
}
function unset($s) {
if (Test-Path "env:$s") {
Remove-Item "env:$s"
}
if (Get-Variable -Name $s -ErrorAction SilentlyContinue) {
Remove-Variable -Name $s -Scope Global
}
}
function Source-Anything($path) {
$tempFile = (New-TemporaryFile).FullName + ".ps1"
Copy-Item $path $tempFile
. $tempFile
}
# MAYBE some magic 'not interactive pw so skip the theme stuff???' and return
Set-PSReadlineOption -BellStyle Visual
if (Get-Command rg -errorAction SilentlyContinue) {
$env:RIPGREP_CONFIG_PATH = (Join-Path $PSScriptRoot .ripgreprc)
}
New-Alias time Measure-Command # MAYBE switch to function { Measure-Command { & $args } } in order to emulate bash time builtin
function timeit($func) { 0..3 | % { (time $func).TotalMilliseconds } } # MAYBE add a -warm switch, that runs once, printing the output
If (Test-Path Alias:md) { Remove-Item Alias:md }
function md($dir) { mkdir -p $dir | out-null; cd $dir }
function PrependPATH($s) {
$sep = [IO.Path]::PathSeparator
if ($s.Contains($sep)) { throw "PATH must be a single directory, got: $s" }
if ([string]::IsNullOrWhiteSpace($ENV:PATH)) { throw "ENV:PATH is empty; refusing to modify" }
$s = convert-path $s # https://github.com/ansible/ansible-lint/issues/2688#issuecomment-1944316451
$pathParts = ($ENV:PATH -split $sep) | Where-Object { $_ -ne $s }
$env:PATH = $s + $sep + ($pathParts -join $sep)
}
function AppendPATH($s) {
$sep = [IO.Path]::PathSeparator
if ($s.Contains($sep)) { throw "PATH must be a single directory, got: $s" }
if ([string]::IsNullOrWhiteSpace($ENV:PATH)) { throw "ENV:PATH is empty; refusing to modify" }
$s = convert-path $s
$pathParts = ($ENV:PATH -split $sep) | Where-Object { $_ -ne $s }
$env:PATH = ($pathParts -join $sep) + $sep + $s
}
PrependPATH $PSScriptRoot
if (gcm Set-PoshPrompt -ErrorAction SilentlyContinue) {
Write-Warning "Stop using pwsh module! https://ohmyposh.dev/docs/migrating"
}
if (gcm mise -ErrorAction SilentlyContinue) {
(&mise activate pwsh) | Out-String | Invoke-Expression
}
# Run tools AFTER mise updates PATH
if (gcm oh-my-posh -ErrorAction SilentlyContinue) {
$env:VIRTUAL_ENV_DISABLE_PROMPT = "yes" # Skip venv prompt, because custom prompt sets it
Import-Module posh-git
oh-my-posh init pwsh --config (Join-Path $PSScriptRoot .go-my-posh.yaml) | Invoke-Expression
}
if (Get-Command gh -all -ErrorAction SilentlyContinue) {
Invoke-Expression -Command $(gh completion -s powershell | Out-String)
}
if ($IsStandardLaunch) { quotation }