-
-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
224 lines (189 loc) · 7.99 KB
/
Microsoft.PowerShell_profile.ps1
File metadata and controls
224 lines (189 loc) · 7.99 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
### Chris Titus Tech's PowerShell profile
$poshTheme = if (-not [string]::IsNullOrWhiteSpace($env:POSH_THEME)) {
$env:POSH_THEME
} else {
Join-Path $Home 'cobalt2.omp.json'
}
if (Get-Module -ListAvailable -Name Terminal-Icons) {
Import-Module -Name Terminal-Icons
} else {
Write-Warning "Terminal-Icons module not found."
}
Write-Host "Use 'Show-Help' to list all available functions" -ForegroundColor Yellow
# History & Colors
Set-PSReadLineOption -PredictionViewStyle ListView -Colors @{
Command = '#87CEEB'
Parameter = '#98FB98'
Operator = '#FFB6C1'
Variable = '#DDA0DD'
String = '#FFDAB9'
Number = '#B0E0E6'
Type = '#F0E68C'
Comment = '#D3D3D3'
Keyword = '#8367c7'
Error = '#FF6347'
}
#KeyBinds
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -Function DeleteChar
Set-PSReadLineKeyHandler -Chord 'Ctrl+w' -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Chord 'Alt+d' -Function DeleteWord
Set-PSReadLineKeyHandler -Chord 'Ctrl+LeftArrow' -Function BackwardWord
Set-PSReadLineKeyHandler -Chord 'Ctrl+RightArrow' -Function ForwardWord
Set-PSReadLineKeyHandler -Chord 'Ctrl+z' -Function Undo
Set-PSReadLineKeyHandler -Chord 'Ctrl+y' -Function Redo
# Functions
function Update-Profile {
Invoke-WebRequest -Uri https://github.com/ChrisTitusTech/powershell-profile/raw/main/Microsoft.PowerShell_profile.ps1 -OutFile $Profile
Write-Host "Updated PowerShell Profile" -ForegroundColor Green
}
# File / Directory Utilities
function touch ($File) {
if (Test-Path $File) {
(Get-Item $File).LastWriteTime = Get-Date
} else {
New-Item $File -ItemType File | Out-Null
}
}
function mkcd ($Path) {
New-Item -Path $Path -ItemType Directory -Force | Out-Null
Set-Location -Path $Path
}
function trash ($Path) {
if (Test-Path $Path -PathType Container) {
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($Path,'OnlyErrorDialogs','SendToRecycleBin')
} else {
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($Path,'OnlyErrorDialogs','SendToRecycleBin')
}
}
function ff ($Name) {
Get-ChildItem -Recurse -Filter $Name -File | Select-Object -ExpandProperty FullName
}
function head ($Path) {
Get-Content $Path -Head 10
}
function sed ($File, $Find, $Replace) {
(Get-Content $File).replace("$Find", $Replace) | Set-Content $file
}
function which ($Name) {
(Get-Command $Name).Source
}
function pgrep ($Name) {
Get-Process -Name $Name -ErrorAction SilentlyContinue
}
function pkill ($Name) {
Get-Process -Name $Name -ErrorAction SilentlyContinue | Stop-Process -Force
}
function k9 ($Name) {
pkill $Name
}
# System Utilities
function uptime {
(Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime | Select-Object Days, Hours, Minutes, Seconds
}
function winutil {
Invoke-RestMethod https://christitus.com/win | Invoke-Expression
}
function winutildev {
Invoke-RestMethod https://christitus.com/windev | Invoke-Expression
}
# Git Shortcuts
function gs { git status }
function ga { git add . }
function gp { git push }
function gpush { git push }
function gpull { git pull }
function gcl { git clone $args }
function g { __zoxide_z github }
function gcom {
git add .
git commit -m "$args"
}
function lazyg {
git add .
git commit -m "$args"
git push
}
function docs {
Set-Location -Path ([Environment]::GetFolderPath("MyDocuments"))
}
# Listing / Viewing
function la {
Get-ChildItem | Format-Table -AutoSize
}
function ll {
Get-ChildItem -Force | Format-Table -AutoSize
}
# Aliases
Set-Alias -Name unzip -Value Expand-Archive
Set-Alias -Name grep -Value Select-String
# Help Function
function Show-Help {
$title = $PSStyle.Foreground.BrightMagenta
$section = $PSStyle.Foreground.BrightBlue
$command = $PSStyle.Foreground.BrightGreen
$desc = $PSStyle.Foreground.BrightWhite
$accent = $PSStyle.Foreground.BrightYellow
$dim = $PSStyle.Foreground.BrightBlack
$reset = $PSStyle.Reset
Write-Host @"
${title} PowerShell Profile Help${reset}
${dim}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${reset}
${section} Update${reset}
${command}Update-Profile${reset} ${accent}→${reset} ${desc}Updates the profile from a remote repository.${reset}
${section} Git Shortcuts${reset}
${dim}────────────────────────────────────────────────────${reset}
${command}g${reset} ${accent}→${reset} ${desc}Changes to the GitHub directory${reset}
${command}ga${reset} ${accent}→${reset} ${desc}git add .${reset}
${command}gcl <repo>${reset} ${accent}→${reset} ${desc}git clone${reset}
${command}gcom <message>${reset} ${accent}→${reset} ${desc}add + commit${reset}
${command}gp / gpush${reset} ${accent}→${reset} ${desc}git push${reset}
${command}gpull${reset} ${accent}→${reset} ${desc}git pull${reset}
${command}gs${reset} ${accent}→${reset} ${desc}git status${reset}
${command}lazyg <message>${reset} ${accent}→${reset} ${desc}add + commit + push${reset}
${section} System Shortcuts${reset}
${dim}────────────────────────────────────────────────────${reset}
${command}docs${reset} ${accent}→${reset} ${desc}Documents folder${reset}
${command}ff <name>${reset} ${accent}→${reset} ${desc}Search files${reset}
${command}grep <pattern> [path]${reset} ${accent}→${reset} ${desc}Search text${reset}
${command}head <file>${reset} ${accent}→${reset} ${desc}First lines${reset}
${command}k9 <name>${reset} ${accent}→${reset} ${desc}Kill process by name${reset}
${command}ll${reset} ${accent}→${reset} ${desc}List files${reset}
${command}mkcd <dir>${reset} ${accent}→${reset} ${desc}Create + enter dir${reset}
${command}pgrep <name>${reset} ${accent}→${reset} ${desc}Find process by name${reset}
${command}pkill <name>${reset} ${accent}→${reset} ${desc}Stop process by name${reset}
${command}sed <file> <find> <replace>${reset} ${accent}→${reset} ${desc}Replace text${reset}
${command}touch <file>${reset} ${accent}→${reset} ${desc}Create file${reset}
${command}unzip <file>${reset} ${accent}→${reset} ${desc}Extract zip${reset}
${command}uptime${reset} ${accent}→${reset} ${desc}System uptime${reset}
${command}which <name>${reset} ${accent}→${reset} ${desc}Locate command${reset}
${command}winutil${reset} ${accent}→${reset} ${desc}Run WinUtil${reset}
${command}winutildev${reset} ${accent}→${reset} ${desc}Run WinUtil Dev${reset}
${dim}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${reset}
"@
}
$shouldInitPosh = $false
if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) {
if (Test-Path $poshTheme) {
$shouldInitPosh = $true
} else {
Write-Warning "oh-my-posh theme not found at $poshTheme."
}
} else {
Write-Warning "oh-my-posh is not installed."
}
$shouldInitZoxide = $false
if (Get-Command zoxide -ErrorAction SilentlyContinue) {
$shouldInitZoxide = $true
} else {
Write-Warning "zoxide is not installed."
}
# init commands should be at the end of the profile
if ($shouldInitPosh) {
Invoke-Expression (& { (oh-my-posh init pwsh --config $poshTheme | Out-String) })
}
if ($shouldInitZoxide) {
Invoke-Expression (& { (zoxide init --cmd z powershell | Out-String) })
}