/
Microsoft.PowerShell_profile.ps1
448 lines (371 loc) · 9.03 KB
/
Microsoft.PowerShell_profile.ps1
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# PowerShell 文档 https://learn.microsoft.com/zh-cn/powershell/scripting/learn/ps101/00-introduction?view=powershell-7.4
# 配置文件文档 https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.4
# 比较运算符 https://learn.microsoft.com/zh-cn/powershell/scripting/learn/ps101/05-formatting-aliases-providers-comparison?view=powershell-7.4#comparison-operators
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
# -------------------------------------------------- #
# 主题设置
# -------------------------------------------------- #
# ------------------------- #
# oh-my-posh
# ------------------------- #
# https://ohmyposh.dev/
# 设置 Theme 目录下随机主题
# $theme = Get-ChildItem $env:POSH_THEMES_PATH | Get-Random
# Write-Output "Hello! Today's lucky theme is $theme :)"
# oh-my-posh init pwsh --config $theme | Invoke-Expression
# ------------------------- #
# starship
# ------------------------- #
# https://starship.rs/zh-CN/
Invoke-Expression (&starship init powershell)
# 开启文件/文件夹图标 https://github.com/devblackops/Terminal-Icons
Import-Module -Name Terminal-Icons
# -------------------------------------------------- #
# 导入插件
# -------------------------------------------------- #
# ------------------------- #
# PSReadLine
# ------------------------- #
# https://github.com/PowerShell/PSReadLine
# 设置 Tab 为补全的快捷键
# Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
# 设置 Ctrl + Z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
# ------------------------- #
# 开启 git 命令补全
# ------------------------- #
# https://github.com/dahlbyk/posh-git
# Import-Module posh-git
# ------------------------- #
# z
# ------------------------- #
# https://github.com/badmotorfinger/z
Import-Module z
# ------------------------- #
# 语法高亮
# ------------------------- #
Import-Module syntax-highlighting
# ------------------------- #
# 修复 ni 命令被占用
# ------------------------- #
# https://github.com/antfu/ni
Remove-Item Alias:ni -Force -ErrorAction Ignore
$profileEntry = 'Remove-Item Alias:ni -Force -ErrorAction Ignore'
$profileContent = Get-Content $profile
if ($profileContent -notcontains $profileEntry) {
("`n" + $profileEntry) | Out-File $profile -Append -Force -Encoding UTF8
}
# -------------------------------------------------- #
# 自定义别名
# -------------------------------------------------- #
# https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.utility/set-alias?view=powershell-7.3
# ------------------------- #
# gsudo
# winget install gerardog.gsudo
# ------------------------- #
# https://github.com/gerardog/gsudo/
Set-Alias sudo gsudo
# ------------------------- #
# Efficient Operation
# ------------------------- #
# 查看环境变量
Set-Alias env CatEnv
function CatEnv {
Get-ChildItem Env: | Format-Table -AutoSize
}
# 打开配置
Set-Alias cmua OpenPROFile
function OpenPROFile {
if (Get-Command vim -ErrorAction SilentlyContinue) {
vim $PROFILE
}
elseif (Get-Command nvim -ErrorAction SilentlyContinue) {
nvim $PROFILE
}
elseif (Get-Command code -ErrorAction SilentlyContinue) {
code $PROFILE
}
else {
notepad $PROFILE
}
}
# 重新加载配置
Set-Alias reload ReloadProfile
function ReloadProfile {
. $PROFILE
}
# 返回 home 目录
Set-Alias ~ GoHome
function GoHome {
Set-Location $env:USERPROFILE
}
# cd 到 project 目录/指定目录
Set-Alias project GoProject
function GoProject([string]$catalog) {
if ([string]::IsNullOrWhiteSpace($catalog)) {
Set-Location $env:USERPROFILE/project
}
else {
$path = "$env:USERPROFILE/project/$catalog"
if ($path -and (Test-Path $path -PathType Container)) {
Set-Location $path
}
else {
Write-Host "路径无效,无法进行跳转"
}
}
}
# 退出
Set-Alias q UseExit
function UseExit {
exit
}
Set-Alias ip ipconfig
# 使用 VS Code 打开目录/文件
Set-Alias c OpenFile
function OpenFile {
code .
}
# 使用 explorer 打开目录
Set-Alias e. OpenExplorer
function OpenExplorer {
explorer .
}
Set-Alias helloworld Hello
function Hello([string]$text = "Hello, World!") {
npx figlet $text
}
# ------------------------- #
# nvim
# ------------------------- #
Set-Alias vi nvim
Set-Alias vim nvim
# ------------------------- #
# Java run aliases
# ------------------------- #
Set-Alias mrun MvnRun
function MvnRun {
mvn tomcat7:run @Args
}
Set-Alias mc MvnClean
function MvnClean {
mvn clean @Args
}
Set-Alias mcu MvnCleanInstall
function MvnCleanInstall {
mvn clean install -e -U @Args
}
Set-Alias pro MvnProOut
function MvnProOut {
mvn tomcat7:run -P pro-out @Args
}
Set-Alias out MvnOut
function MvnOut {
mvn tomcat7:run -P out @Args
}
# ------------------------- #
# Node Package Manager
# ------------------------- #
# https://github.com/antfu/ni
Set-Alias r NrLastModel
function NrLastModel {
nr -
}
Set-Alias d NrDev
function NrDev {
nr dev @Args
}
Set-Alias p NrPlay
function NrPlay {
nr play @Args
}
Set-Alias s NrStart
function NrStart {
nr start @Args
}
Set-Alias b NrBuild
function NrBuild {
nr build @Args
}
Set-Alias bw NrBuildWatch
function NrBuildWatch {
nr build --watch @Args
}
Set-Alias h5 NrDevH5
function NrDevH5 {
nr dev:h5 @Args
}
Set-Alias weixin NrDevMpWeixin
function NrDevMpWeixin {
nr dev:mp-weixin @Args
}
Set-Alias t NrTest
function NrTest {
nr test @Args
}
Set-Alias tu NrTestU
function NrTestU {
nr test -u @Args
}
Set-Alias tw NrTestWatch
function NrTestWatch {
nr test --watch @Args
}
Set-Alias lint NrLint
function NrLint {
nr lint @Args
}
Set-Alias lintf NrLintFix
function NrLintFix {
nr lint --fix @Args
}
Set-Alias up NpmUpgrade
function NpmUpgrade {
npx taze major -wir
}
# ------------------------- #
# Git aliases
# ------------------------- #
Set-Alias ginit GitInit
function GitInit {
git init
}
Set-Alias gcuinit Set-GitUser
function Set-GitUser {
$currentCulture = $PSCulture.Name
$useChinesePrompt = $currentCulture -eq "zh-CN"
$prompt = if ($useChinesePrompt) {
@("设置 Git 用户信息。", "请输入您的姓名:", "请输入您的邮箱:", "Git 用户信息已设置。", "姓名和邮箱不能为空。Git 用户信息未更新.")
}
else {
@("Setting up Git user information.", "Enter your name: ", "Enter your email: ", "Git user information has been set.", "Name and email cannot be empty. Git user information has not been updated.")
}
Write-Host $prompt[0]
$UserName = Read-Host $prompt[1]
$UserEmail = Read-Host $prompt[2]
if (-not [string]::IsNullOrWhiteSpace($UserName) -and -not [string]::IsNullOrWhiteSpace($UserEmail)) {
git config --global user.name $UserName
git config --global user.email $UserEmail
Write-Host $prompt[3]
Write-Host "Your name: $UserName"
Write-Host "Your email: $UserEmail"
}
else {
Write-Host $prompt[4]
}
}
# Go to project root
Set-Alias grt GitRoot
function GitRoot {
$path = $(git rev-parse --show-toplevel)
# 这里需要做个判断,$path 是否获取成功,失败就不跳转
try {
if ($path -and (Test-Path $path -PathType Container)) {
Set-Location $path
}
else {
Write-Host "路径无效,无法进行跳转"
}
}
catch {
Write-Host "处理路径时出现错误: $_"
}
}
Set-Alias gpl GitPull
function GitPull {
git pull @Args
}
Set-Alias gp GitPush -Force
function GitPush {
git push @Args
}
Set-Alias gs GitStatus
function GitStatus {
git status @Args
}
Set-Alias gl GitLog -Force
function GitLog {
git log @Args
}
Set-Alias glo GitLogOnline
function GitLogOnline {
git log --oneline --graph @Args
}
Set-Alias ga GitAdd
function GitAdd {
if ($Args.Count -eq 0) {
git add .
}
else {
git add @Args
}
}
Set-Alias ga. GitAddAll
function GitAddAll {
git add -A
}
Set-Alias gcm GitCommit -Force
function GitCommit {
git commit -m @Args
}
Set-Alias gam GitAddCommit
function GitAddCommit {
git add -A && git commit -m @Args
}
Set-Alias gcl GitClone
function GitClone {
git clone @Args
}
Set-Alias gsh GitStash
function GitStash {
git stash @Args
}
Set-Alias gshp GitStashPop
function GitStashPop {
git stash pop
}
Set-Alias gb GitBranch
function GitBranch {
git branch @Args
}
Set-Alias gbd GitBranchDelete
function GitBranchDelete {
git branch -d @Args
}
Set-Alias gme GitMerge
function GitMerge {
git merge @Args
}
Set-Alias gc GitCheckout -Force
function GitCheckout {
git checkout @Args
}
Set-Alias gcb GitCheckoutBranch
function GitCheckoutBranch {
git checkout -b @Args
}
Set-Alias master GitCheckoutMaster
Set-Alias main GitCheckoutMaster
function GitCheckoutMaster {
$branchList = git branch --list "master"
$mainBranchList = git branch --list "main"
if ($branchList -and $mainBranchList) {
git checkout master
}
elseif ($mainBranchList) {
git checkout main
}
else {
git checkout master
}
}
Set-Alias dev GitCheckoutDev
function GitCheckoutDev {
git checkout develop
}
Set-Alias grh GitReset
function GitReset {
git reset HEAD~1
}