美化 PowerShell and Windows Terminal

美化 PowerShell

安装插件到所有用户

以管理员身份打开 PowerShell

安装posh-git

1
Install-Module posh-git

安装oh-my-posh

参考 install in windows 配置

1
winget install JanDeDobbeleer.OhMyPosh -s winget

这会自动下载安装ohmyposh 只会设置 POSH_THEMES_PATH 这个环境变量 而不会把oh-my-posh 的bin 目录加入到PATH
所以在配置文件中需要使用绝对路径

安装PSReadLine

1
Install-Module -Name PSReadLine -Scope CurrentUser -Force -SkipPublisherCheck

设置启动文件应用主题

1
  notepad.exe $Profile

profile.ps1 输入以下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$Env:POSHGIT_CYGWIN_WARNING = 'off'   #  WARNING: You appear to have an unsupported Git distribution; setting $GitPromptSettings.AnsiConsole = $false. posh-git recommends Git for Windows.
Import-Module posh-git # 引入 posh-git
Import-Module oh-my-posh # 引入 oh-my-posh
# Set-Theme Paradox # 设置主题为 Paradox  后来 Set-Theme 改名为 Set-PoshPrompt
# C:\Users\wcq\AppData\Local\Programs\oh-my-posh\bin\oh-my-posh.exe init pwsh --config "$env:POSH_THEMES_PATH\paradox.omp.json" | Invoke-Expression
C:\Users\wcq\AppData\Local\Programs\oh-my-posh\bin\oh-my-posh.exe init pwsh --config "$env:POSH_THEMES_PATH\gmay.omp.json" | Invoke-Expression

# 新版的 PSReadLine 已经弃用PredictionSource 选项
# Set-PSReadLineOption -PredictionSource History # 设置预测文本来源为历史记录
Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$true    # 当使用上下键查看历史记录时 光标位于命令末尾
Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录

设置字体

重启 PowerShell 生效:


命令补全不会自己显示出来 需要按方向 上 下键才能显示出来

美化 Windows Terminal

安装字体

安装 Fira Code 字体

安装Windows Terminal

直接在 Microsoft store 搜索安装

启动文件

打开 Windows Terminal

1
echo $Profile

可以察看启动文件的位置
把以下内容填入启动文件 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
$Env:POSHGIT_CYGWIN_WARNING = 'off'   #  WARNING: You appear to have an unsupported Git distribution; setting $GitPromptSettings.AnsiConsole = $false. posh-git recommends Git for Windows.
Import-Module posh-git # 引入 posh-git
#Import-Module oh-my-posh # 引入 oh-my-posh
    # Set-Theme Paradox # 设置主题为 Paradox  后来 Set-Theme 改名为 Set-PoshPrompt
# C:\Users\wcq\AppData\Local\Programs\oh-my-posh\bin\oh-my-posh.exe init pwsh --config "$env:POSH_THEMES_PATH\paradox.omp.json" | Invoke-Expression
C:\Users\wcq\AppData\Local\Programs\oh-my-posh\bin\oh-my-posh.exe init pwsh --config "$env:POSH_THEMES_PATH\gmay.omp.json" | Invoke-Expression

# Set-PSReadLineOption -PredictionSource History # 设置预测文本来源为历史记录
Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$true
Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录

iex ($(lua "E:/Users/wcq/AppData/Roaming/SPB_Data/z.lua/z.lua" --init powershell) -join "`n")

# alias zc='z -c'      # 严格匹配当前路径的子路径
# alias zz='z -i'      # 使用交互式选择模式
# alias zf='z -I'      # 使用 fzf 对多个结果进行选择
# alias zb='z -b'      # 快速回到父目录
# alias zh='z -I -t .' # 查看数据库中的所有路径情况

function zb { z -b }
function zf { z -I }
function zz { z -i }
function zh { z -I -t . }

function e { explorer.exe $args }     # powershell 输入 e . Enter在资源管理器中打开当前目录
# Import-Module ZLocation

function rgb { rg --binary $args}

添加右键菜单

  • 打开注册表编辑器

  • 找到 HKEY_CLASS_ROOT\Directory\Background\shell

  • 在 shell 上右键 新建一个项 名为 WindowsTerninal

  • 在 WindowsTerninal 上右键 新建一个项 名为 command

  • 选中 command 在右侧窗口双击默认在数值数据中填入下面字符串

    1
    
      C:\Users\wcq\AppData\Local\Microsoft\WindowsApps\wt.exe
    

    其中 wcq 为我自己的用户名 视具体情况修改

配置

使用任意工具打开 如 sublime text3
把下面内容覆盖掉原来的内容:

  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
{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "alwaysShowTabs" : true,
    "copyOnSelect" : true,
    "defaultProfile" : "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "initialCols" : 120,
    "initialRows" : 30,
    "keybindings" :
    [
        {
            "command" : "closePane",
            "keys" :
            [
                "ctrl+shift+w"
            ]
        },
        {
            "command" : "copy",
            "keys" :
            [
                "ctrl+shift+c"
            ]
        },
        {
            "command" : "duplicateTab",
            "keys" :
            [
                "ctrl+shift+d"
            ]
        },
        {
            "command" : "newTab",
            "keys" :
            [
                "ctrl+shift+t"
            ]
        },
        {
            "command" : "newTabProfile0",
            "keys" :
            [
                "ctrl+shift+1"
            ]
        },
        {
            "command" : "newTabProfile1",
            "keys" :
            [
                "ctrl+shift+2"
            ]
        },
        {
            "command" : "newTabProfile2",
            "keys" :
            [
                "ctrl+shift+3"
            ]
        },
        {
            "command" : "newTabProfile3",
            "keys" :
            [
                "ctrl+shift+4"
            ]
        },
        {
            "command" : "newTabProfile4",
            "keys" :
            [
                "ctrl+shift+5"
            ]
        },
        {
            "command" : "newTabProfile5",
            "keys" :
            [
                "ctrl+shift+6"
            ]
        },
        {
            "command" : "newTabProfile6",
            "keys" :
            [
                "ctrl+shift+7"
            ]
        },
        {
            "command" : "newTabProfile7",
            "keys" :
            [
                "ctrl+shift+8"
            ]
        },
        {
            "command" : "newTabProfile8",
            "keys" :
            [
                "ctrl+shift+9"
            ]
        },
        {
            "command" : "nextTab",
            "keys" :
            [
                "ctrl+tab"
            ]
        },
        {
            "command" : "openNewTabDropdown",
            "keys" :
            [
                "ctrl+shift+space"
            ]
        },
        {
            "command" : "openSettings",
            "keys" :
            [
                "ctrl+,"
            ]
        },
        {
            "command" : "paste",
            "keys" :
            [
                "ctrl+shift+v"
            ]
        },
        {
            "command" : "prevTab",
            "keys" :
            [
                "ctrl+shift+tab"
            ]
        },
        {
            "command" : "scrollDown",
            "keys" :
            [
                "ctrl+shift+down"
            ]
        },
        {
            "command" : "scrollDownPage",
            "keys" :
            [
                "ctrl+shift+pgdn"
            ]
        },
        {
            "command" : "scrollUp",
            "keys" :
            [
                "ctrl+shift+up"
            ]
        },
        {
            "command" : "scrollUpPage",
            "keys" :
            [
                "ctrl+shift+pgup"
            ]
        },
        {
            "command" : "switchToTab0",
            "keys" :
            [
                "ctrl+alt+1"
            ]
        },
        {
            "command" : "switchToTab1",
            "keys" :
            [
                "ctrl+alt+2"
            ]
        },
        {
            "command" : "switchToTab2",
            "keys" :
            [
                "ctrl+alt+3"
            ]
        },
        {
            "command" : "switchToTab3",
            "keys" :
            [
                "ctrl+alt+4"
            ]
        },
        {
            "command" : "switchToTab4",
            "keys" :
            [
                "ctrl+alt+5"
            ]
        },
        {
            "command" : "switchToTab5",
            "keys" :
            [
                "ctrl+alt+6"
            ]
        },
        {
            "command" : "switchToTab6",
            "keys" :
            [
                "ctrl+alt+7"
            ]
        },
        {
            "command" : "switchToTab7",
            "keys" :
            [
                "ctrl+alt+8"
            ]
        },
        {
            "command" : "switchToTab8",
            "keys" :
            [
                "ctrl+alt+9"
            ]
        }
    ],
    "requestedTheme" : "system",
    "showTabsInTitlebar" : true,
    "showTerminalTitleInTitlebar" : true,
    "wordDelimiters" : " ./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}~?\u2502"
    ,
    "profiles" :
    [
        {
            "acrylicOpacity" : 1.0,
            "background" : "#012456",
            "closeOnExit" : true,
            "colorScheme" : "Campbell",
            "commandline" : "powershell.exe",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "filledBox",
            "fontFace" : "MesloLGM NF",
            "fontSize" : 14,
            "guid" : "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
            "historySize" : 9001,
            "icon" : "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
            "name" : "Windows PowerShell",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : ".",
            "useAcrylic" : false
        },
        {
            "acrylicOpacity" : 0.95,
            "closeOnExit" : true,
            "colorScheme" : "Campbell",
            "commandline" : "cmd.exe",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "MesloLGM NF",
            "fontSize" : 14,
            "guid" : "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
            "historySize" : 9001,
            "icon" : "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png",
            "name" : "cmd",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : ".",
            "useAcrylic" : false
        },
        {
            "acrylicOpacity" : 0.59999999999999998,
            "closeOnExit" : false,
            "colorScheme" : "Vintage",
            "commandline" : "Azure",
            "connectionType" : "{d9fcfdfa-a479-412c-83b7-c5640e61cd62}",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "MesloLGM NF",
            "fontSize" : 14,
            "guid" : "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
            "historySize" : 9001,
            "icon" : "ms-appx:///ProfileIcons/{b453ae62-4e3d-5e58-b989-0a998ec441b8}.png",
            "name" : "Azure Cloud Shell",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : "%USERPROFILE%",
            "useAcrylic" : true
        },
        {
            "acrylicOpacity" : 0.5,
            "closeOnExit" : true,
            "colorScheme" : "Campbell",
            "commandline" : "wsl.exe -d Ubuntu-18.04",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "MesloLGM NF",
            "fontSize" : 14,
            "guid" : "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
            "historySize" : 9001,
            "icon" : "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
            "name" : "Ubuntu-18.04",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : ".",
            "useAcrylic" : false
        }
    ],
    "schemes" :
    [
        {
            "background" : "#0C0C0C",
            "black" : "#0C0C0C",
            "blue" : "#0037DA",
            "brightBlack" : "#767676",
            "brightBlue" : "#3B78FF",
            "brightCyan" : "#61D6D6",
            "brightGreen" : "#16C60C",
            "brightPurple" : "#B4009E",
            "brightRed" : "#E74856",
            "brightWhite" : "#F2F2F2",
            "brightYellow" : "#F9F1A5",
            "cyan" : "#3A96DD",
            "foreground" : "#CCCCCC",
            "green" : "#13A10E",
            "name" : "Campbell",
            "purple" : "#881798",
            "red" : "#C50F1F",
            "white" : "#CCCCCC",
            "yellow" : "#C19C00"
        },
        {
            "background" : "#000000",
            "black" : "#000000",
            "blue" : "#000080",
            "brightBlack" : "#808080",
            "brightBlue" : "#0000FF",
            "brightCyan" : "#00FFFF",
            "brightGreen" : "#00FF00",
            "brightPurple" : "#FF00FF",
            "brightRed" : "#FF0000",
            "brightWhite" : "#FFFFFF",
            "brightYellow" : "#FFFF00",
            "cyan" : "#008080",
            "foreground" : "#C0C0C0",
            "green" : "#008000",
            "name" : "Vintage",
            "purple" : "#800080",
            "red" : "#800000",
            "white" : "#C0C0C0",
            "yellow" : "#808000"
        },
        {
            "background" : "#282C34",
            "black" : "#282C34",
            "blue" : "#61AFEF",
            "brightBlack" : "#5A6374",
            "brightBlue" : "#61AFEF",
            "brightCyan" : "#56B6C2",
            "brightGreen" : "#98C379",
            "brightPurple" : "#C678DD",
            "brightRed" : "#E06C75",
            "brightWhite" : "#DCDFE4",
            "brightYellow" : "#E5C07B",
            "cyan" : "#56B6C2",
            "foreground" : "#DCDFE4",
            "green" : "#98C379",
            "name" : "One Half Dark",
            "purple" : "#C678DD",
            "red" : "#E06C75",
            "white" : "#DCDFE4",
            "yellow" : "#E5C07B"
        },
        {
            "background" : "#FAFAFA",
            "black" : "#383A42",
            "blue" : "#0184BC",
            "brightBlack" : "#4F525D",
            "brightBlue" : "#61AFEF",
            "brightCyan" : "#56B5C1",
            "brightGreen" : "#98C379",
            "brightPurple" : "#C577DD",
            "brightRed" : "#DF6C75",
            "brightWhite" : "#FFFFFF",
            "brightYellow" : "#E4C07A",
            "cyan" : "#0997B3",
            "foreground" : "#383A42",
            "green" : "#50A14F",
            "name" : "One Half Light",
            "purple" : "#A626A4",
            "red" : "#E45649",
            "white" : "#FAFAFA",
            "yellow" : "#C18301"
        },
        {
            "background" : "#002B36",
            "black" : "#073642",
            "blue" : "#268BD2",
            "brightBlack" : "#002B36",
            "brightBlue" : "#839496",
            "brightCyan" : "#93A1A1",
            "brightGreen" : "#586E75",
            "brightPurple" : "#6C71C4",
            "brightRed" : "#CB4B16",
            "brightWhite" : "#FDF6E3",
            "brightYellow" : "#657B83",
            "cyan" : "#2AA198",
            "foreground" : "#839496",
            "green" : "#859900",
            "name" : "Solarized Dark",
            "purple" : "#D33682",
            "red" : "#DC322F",
            "white" : "#EEE8D5",
            "yellow" : "#B58900"
        },
        {
            "background" : "#FDF6E3",
            "black" : "#073642",
            "blue" : "#268BD2",
            "brightBlack" : "#002B36",
            "brightBlue" : "#839496",
            "brightCyan" : "#93A1A1",
            "brightGreen" : "#586E75",
            "brightPurple" : "#6C71C4",
            "brightRed" : "#CB4B16",
            "brightWhite" : "#FDF6E3",
            "brightYellow" : "#657B83",
            "cyan" : "#2AA198",
            "foreground" : "#657B83",
            "green" : "#859900",
            "name" : "Solarized Light",
            "purple" : "#D33682",
            "red" : "#DC322F",
            "white" : "#EEE8D5",
            "yellow" : "#B58900"
        }
    ]
}

profiles 中的部分属性:

  • “startingDirectory”
    “.”, 意思从当前目录打开 PowerShell
  • acrylicOpacity ( powershell设置不透明度 )
    是不透明度: 如果为1 则不透明
  • “useAcrylic”
    字面意思是使用透明度, 看起来是废弃了, 因为即使设成false 还是有透明属性

更改快捷键

默认的在Windwos Terminal 里的翻页是ctrl+shift+pgdn 和 ctrl+shift+pgup
打开配置文件把

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
            "command" : "scrollDownPage",
            "keys" :
            [
                "ctrl+shift+pgdn"
            ]
            
            "command" : "scrollUpPage",
            "keys" :
            [
                "ctrl+shift+pgup"
            ]

里的 ctrl+shift+ 删掉 这样使用PageUp 和PageDown 键就可以翻页了
滚到最前面还是使用 ctrl+shift+Home
滚到最后面还是使用 ctrl+shift+End

效果

打开一个新的PowerShell 窗口

鼠标点 gif 链接在新窗口中打开 按Enter 键可预览 gif
里面先输入 p 再按方向上下键补全命令

windows terminal 或者 powershell 出现光标不连续

如上图所示, aaaaaaaaa 应该紧接着aa 的

原因

oh-my-posh 有问题

解决

更新oh-my-posh
参考 安装oh-my-posh