-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
358 lines (280 loc) · 10.7 KB
/
Microsoft.PowerShell_profile.ps1
File metadata and controls
358 lines (280 loc) · 10.7 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# ===========
# ENVIRONMENT
# ===========
$Env:SHELL = "pwsh"
$Env:EDITOR = "code"
# =======
# MODULES
# =======
# MY MODULES
# ==========
# https://www.github.com/Shresht7/PowerShell
# Utilities is a collection of useful PowerShell functions.
Import-Module -Name Utilities
# FSUtils is a collection of useful filesystem utility functions.
Import-Module -Name FSUtils
# PSReadlineUtilities is a collection of useful PSReadline functions.
Import-Module -Name PSReadlineUtils
# PSFavorite provides a simple way to manage and use your favorite PowerShell commands.
Import-Module -Name PSFavorite
Initialize-PSFavorite
# PREDICTOR MODULES
# -----------------
Import-Module -Name CompletionPredictor
# NLPowerShell provides LLM assistance to interactive with PowerShell using natural language
$Env:LLM_MODEL = "gemma3"
Import-Module -Name NLPowerShell
Initialize-NLPowerShell -Ollama -Model $Env:LLM_MODEL -Temperature 0.3
# =======
# STARTUP
# =======
# Show the time and calendar
Write-Host
go-time
go-time cal
# Show a random quote or tip on startup
$startupActions = @(
{
if (Get-Command quotes -ErrorAction SilentlyContinue) {
quotes --filepath (Resolve-Path "$HOME\Data\quotes.csv") --quotes --style 'italic' --no-borders
}
},
{
if (Get-Command tipsc -ErrorAction SilentlyContinue) {
tipsc
}
Write-Host
}
)
try {
Get-Random $startupActions | ForEach-Object { & $_ }
}
catch {
Write-Verbose "Random startup action failed: $_"
}
# ALIASES
# =======
Set-Alias ~ Set-LocationToHome
Set-Alias cdf Set-LocationFuzzy
Set-Alias android scrcpy
Set-Alias gitstatus Invoke-FuzzyGitStatus
Set-Alias gitbranch Invoke-PsFzfGitBranches
Set-Alias gitfiles Invoke-PSFzfGitFiles
Set-Alias githashes Invoke-PSFzfGitHashes
Set-Alias gitstashes Invoke-PsFzfGitStashes
Set-Alias gittags Invoke-PsFzfGitTags
# EXTERNAL
# ========
# ----------
# Oh-My-Posh
# ----------
# Oh-My-Posh is a prompt theme engine for PowerShell
# https://ohmyposh.dev/
oh-my-posh init pwsh --config "~/Configs/Oh-My-Posh/Themes/s7.omp.yaml" | Invoke-Expression
Set-Alias omp oh-my-posh.exe
# ----
# Find
# ----
# fd is a simple, fast and user-friendly alternative to find.
# fd allows you to quickly find files and directories on your system.
# https://github.com/sharkdp/fd
# Alias `fd` as `find`
Set-Alias find fd
# ----------
# Fuzzy Find
# ----------
# fzf is a general-purpose command-line fuzzy finder.
# https://github.com/junegunn/fzf
# Use `fd` instead of `find` in fzf (fuzzy-finder)
$env:FZF_DEFAULT_COMMAND = 'fd --type file'
$env:FZF_DEFAULT_OPTS = '--reverse'
# --------
# Posh-Git
# --------
# posh-git is a PowerShell module that provides Git/PowerShell integration.
# https://github.com/dahlbyk/posh-git
Import-Module posh-git
# -----
# PSFzf
# -----
# PSFzf is a PowerShell module that provides a wrapper around fzf.
# https://github.com/kelleyma49/PSFzf
Import-Module PSFzf
Set-PsFzfOption -EnableFd -GitKeyBindings 'Ctrl+g' -PSReadlineChordReverseHistory 'Ctrl+r'
# ----------
# PSReadLine
# ----------
# https://github.com/PowerShell/PSReadLine
# Usage: https://github.com/PowerShell/PSReadLine#usage
# Sample Profile: https://github.com/PowerShell/PSReadLine/blob/master/PSReadLine/SamplePSReadLineProfile.ps1
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Chord 'Ctrl+SpaceBar' -ScriptBlock { Invoke-FzfTabCompletion }
# Searching for commands with up/down arrow is really handy. The
# option "moves to end" is useful if you want the cursor at the end
# of the line while cycling through history like it does w/o searching,
# without that option, the cursor will remain at the position it was
# when you used up arrow, which can be useful if you forget the exact
# string you started the search on.
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
# Sometimes you enter a command but realize you forgot to do something else first.
# This binding will let you save that command in the history so you can recall it,
# but it doesn't actually execute.
# It also clears the line with RevertLine so the
# undo stack is reset - though redo will still reconstruct the command line.
Set-PSReadLineKeyHandler -Key Alt+w `
-BriefDescription SaveInHistory `
-LongDescription "Save current line in history but do not execute" `
-ScriptBlock {
param($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line)
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
}
# Sometimes you want to get a property of invoke a member on what you've entered so far
# but you need parenthesis to do that. This binding will help by putting parenthesis around the current selection,
# or if nothing is selected, the whole line.
Set-PSReadLineKeyHandler -Key 'Ctrl+Shift+(' `
-BriefDescription ParenthesizeSelection `
-LongDescription "Put parenthesis around the selection or entire line and move the cursor to after the closing parenthesis" `
-ScriptBlock {
param($key, $arg)
# Get the current selection
$selectionStart = $null
$selectionLength = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$selectionStart, [ref]$selectionLength)
# Get the current line
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
# Parenthesize the selection
if ($selectionStart -ne -1) {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($selectionStart, $selectionLength, '(' + $line.SubString($selectionStart, $selectionLength) + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selectionStart + $selectionLength + 2)
}
# Parenthesize the whole line
else {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $line.Length, '(' + $line + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
}
# Invoke the currently selected expression, or if nothing is selected, the whole line.
Set-PSReadLineKeyHandler -Key "Ctrl+Shift+e" `
-BriefDescription InvokeSelection `
-LongDescription "Invoke the currently selected expression, or if nothing is selected, the whole line" `
-ScriptBlock {
param($key, $arg)
# Get the current selection
$selectionStart = $null
$selectionLength = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$selectionStart, [ref]$selectionLength)
# Get the current line
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
# Invoke the selection and replace it with the result
if ($selectionStart -ge 0) {
$expression = $line.SubString($selectionStart, $selectionLength)
$Result = (Invoke-Expression $expression | Out-String).Trim()
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($selectionStart, $selectionLength, $Result)
}
# Invoke the whole line and replace it with the result
else {
$expression = $line.SubString(0, $line.Length)
$Result = (Invoke-Expression $expression | Out-String).Trim()
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $line.Length, $Result)
}
}
$Env:PSReadLineAddToHistory = $True
# History handler
Set-PSReadLineOption -AddToHistoryHandler {
param([string]$line)
$minimumLength = 3
$sensitive = "password|asplaintext|token|key|secret|hook|webhook"
return ($Env:PSReadLineAddToHistory -eq $True) -and
($line.Length -gt $minimumLength) -and
($line[0] -ne ' ') -and
($line[0] -ne ';') -and
($line -NotMatch $sensitive)
}
# Prevent annoying beeping noises
# Set-PSReadLineOption -BellStyle None
# --------------
# Terminal-Icons
# --------------
# Terminal-Icons is a PowerShell module that displays icons in the console.
# https://www.powershellgallery.com/packages/Terminal-Icons/0.5.0
Import-Module Terminal-Icons
# ------------------
# Z Directory Jumper
# ------------------
# z is a directory jumper for PowerShell.
# It tracks your most used directories and allows you to jump to them quickly.
# https://www.powershellgallery.com/packages/z/1.1.9
Import-Module z
# =============
# Auto-Complete
# =============
# Import Completions Module
Import-Module -Name Completions
# ripgrep
if (Get-Command rg -ErrorAction SilentlyContinue) {
rg --generate complete-powershell | Out-String | Invoke-Expression
}
# bat
if (Get-Command bat -ErrorAction SilentlyContinue) {
bat --completion ps1 | Out-String | Invoke-Expression
}
# GitHub CLI
if (Find-Path -Command gh) {
Invoke-Expression -Command $(gh completion -s powershell | Out-String)
}
# Deno
if (Find-Path -Command deno) {
Invoke-Expression -Command $(deno completions powershell | Out-String)
}
# dotnet
if (Find-Path -Command dotnet) {
dotnet completions script pwsh | Out-String | Invoke-Expression
}
# WinGet
# https://github.com/microsoft/winget-cli/blob/master/doc/Completion.md
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
$Local:word = $wordToComplete.Replace('"', '""')
$Local:ast = $commandAst.ToString().Replace('"', '""')
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# =======
# PSStyle
# =======
# $PSStyle is a global variable that contains the current style settings for PowerShell.
# Run `$PSStyle` to see the current style settings and available properties to customize.
# ---------------------------------------------
$PSAccentColor = $PSStyle.Foreground.BrightCyan
# ---------------------------------------------
# Use the accent color
$PSStyle.Formatting.FormatAccent = $PSAccentColor
$PSStyle.Formatting.TableHeader = $PSAccentColor
# =============
# MISCELLANEOUS
# =============
#f45873b3-b655-43a6-b217-97c00aa0db58 PowerToys CommandNotFound module
if ($IsWindows) {
Import-Module -Name Microsoft.WinGet.CommandNotFound
Import-Module -Name Microsoft.PowerShell.PSAdapter
}
# -------
# ALIASES
# -------
Set-Alias -Name tldrf -Value $HOME\Scripts\Reference\Show-TLDR.ps1
Set-Alias -Name lxym -Value $HOME\Scripts\Reference\Learn-X-in-Y-Minutes.ps1
Set-Alias -Name cht.sh -Value $HOME\Scripts\Reference\Show-CheatSheet.ps1