前言

在这之前,我一直使用的都是 cmder,但是这东西说真的,有点难用。不过好在微软推出了 Windows Terminal,经过我的实测,感觉还是不错的。毕竟颜值才是第一生产力。

image-20220416223843738

开始操作

  1. 下载 Windows Terminal

    下载很简单,在微软商店下载即可。

    image-20210822153753915

  2. 安装一个字体,比较推荐的是JetBrainsMono Nerd Font Mono(可以显示表情)

    如果链接失效请留言或者自行百度搜索。

  3. 安装新款 Powershell Core

    首先声明,我们这儿用的 Powershell 与 Windows 自带的 Powershell 是完全不同的两个东西,除了功能相似和名字相同,两者内在已经天差地别。

    Powershell Core 是什么呢?这是伟大的 .Net Core 跨平台战略的一个重要组成部分,微软设想,要让强大的 .Net 在所有平台上通用,让这么强大的 Powershell 在所有平台上都能用,古老的 bash 可以退休了!

    下载地址(可下载最新版本,不一定要下载图中所示)

  4. 安装 Powershell 插件

    • PSReadLine

      PSReadLine 是一个由微软发布的用于 PowerShell 的行读取实现,提供了以下功能:

      • 语法着色
      • 简单语法错误通知
      • 良好的多行体验
      • 可自定义的键绑定
      • Cmd 和 Emacs 模式
      • 许多配置选项
      • Bash 样式的补全
      • Bash/zsh 样式的交互式历史记录搜索
      • Emacs yank/kill ring
      • 基于 PowerShell Token 的单词移动和删除
      • 撤销/重做
      • 自动保存历史记录,包括在实时会话中共享历史记录
      • 菜单补全、Intellisense

      GitHub 地址:https://github.com/PowerShell/PSReadLine

    • oh-my-posh&posh-git

      类似于 oh-my-zsh,oh-my-posh 为 PowerShell 提供了很多自定义主题和配色,而 posh-git 为 PowerShell 提供了 git 状态显示和命令补全等。

      GitHub 地址:https://github.com/JanDeDobbeleer/oh-my-posh

    看完上边的三个插件介绍,我们来安装这三个插件。

    用管理员模式打开刚装好的新版 powershell,逐行输入命令。

    1
    2
    3
    4
    5
    6
    7
    8
    # 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh
    Install-Module -Name PSReadLine -AllowPrerelease -Force

    # 2. 安装 posh-git 包,让你的 git 更好用
    Install-Module posh-git -Scope CurrentUser

    # 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅
    Install-Module oh-my-posh -Scope CurrentUser

    image-20210822154316987

    一定要用刚刚安装的新版的 powershell(建议使用管理员模式运行),安装过程可能过于慢,请耐心等待。

    后面两个包的来源可能不受系统信任,不用管它,如果让你选择是否信任,直接输入 Y 即可。

  5. 编辑$Profile文件。

    这个文件类似于 ~/.zshrc,会在 PowerShell 启动的时候自动执行,因此我们在这个文件中加载我们所需的模块。

    • Windows

      notepad.exe $Profile

    • Linux

      nano $Profile

    然后在这个文件中添加如下内容:

    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
    #------------------------------- Import Modules BEGIN -------------------------------
    # 引入 posh-git
    Import-Module posh-git

    # 引入 oh-my-posh
    Import-Module oh-my-posh

    # 设置 PowerShell 主题
    Set-PoshPrompt iterm2


    #------------------------------- Import Modules END -------------------------------

    #------------------------------- Set Hot-keys BEGIN -------------------------------
    # 设置 Tab 键补全
    Set-PSReadlineKeyHandler -Key Tab -Function Complete

    # 设置 Ctrl+d 为菜单补全和 Intellisense
    Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

    # 设置 Ctrl+d 为退出 PowerShell
    Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

    # 设置预测文本来源为历史记录
    Set-PSReadLineOption -PredictionSource History

    # 设置 Ctrl+z 为撤销
    Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

    # 设置向上键为后向搜索历史记录
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

    # 设置向下键为前向搜索历史纪录
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
    #------------------------------- Set Hot-keys END -------------------------------

    #------------------------------- Set Alias Begin -------------------------------
    # 1. 编译函数 make
    function MakeThings {
    nmake.exe $args -nologo
    }
    Set-Alias -Name make -Value MakeThings

    # 2. 更新系统 os-update
    Set-Alias -Name os-update -Value Update-Packages

    # 3. 查看目录 ls & ll
    function ListDirectory {
    (Get-ChildItem).Name
    Write-Host("")
    }
    Set-Alias -Name ls -Value ListDirectory
    Set-Alias -Name ll -Value Get-ChildItem
    #------------------------------- Set Alias END -------------------------------

    oh-my-posh 提供了 10 款 漂亮 的主题供我们选择。 Agnoster,Avit,Darkblood,Fish,Honukai,Paradox,PowerLine,robbyrussell,Sorin,tehrob

    可以输入命令Get-PoshThemes来查看所有表情

    image-20220416224208352

  6. 接下来打开Windows Terminal

    Windows Terminal 默认为旧版本的 powershell,我们需要把新版本作为默认。

    修改的内容有三个,如果你不想自己改可以直接将我的粘贴到配置文件(需要注意的是:如果按照默认路径安装(正式版),那么可以直接复制下边。预览版或者更换了安装位置,那么请手动更改位置。其他信息试自己能力进行修改)。

    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
    {
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions": [
    {
    "command": {
    "action": "copy",
    "singleLine": false
    },
    "keys": "ctrl+c"
    },
    {
    "command": "paste",
    "keys": "ctrl+v"
    },
    {
    "command": "find",
    "keys": "ctrl+shift+f"
    },
    {
    "command": {
    "action": "splitPane",
    "split": "auto",
    "splitMode": "duplicate"
    },
    "keys": "alt+shift+d"
    }
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "profiles": {
    "defaults": {},
    "list": [
    {
    "backgroundImage": null,
    "colorScheme": "idleToes",
    "commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo",
    "font": {
    "face": "JetBrainsMono Nerd Font Mono",
    "size": 12
    },
    "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "hidden": false,
    "historySize": 9001,
    "name": "pwsh",
    "opacity": 50,
    "padding": "5, 5, 20, 25",
    "snapOnInput": true,
    "source": "Windows.Terminal.PowershellCore",
    "startingDirectory": ".",
    "useAcrylic": true
    },
    {
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "hidden": true,
    "name": "Windows PowerShell"
    },
    {
    "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
    "hidden": false,
    "name": "\u547d\u4ee4\u63d0\u793a\u7b26"
    },
    {
    "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
    "hidden": true,
    "name": "Azure Cloud Shell",
    "source": "Windows.Terminal.Azure"
    },
    {
    "guid": "{a3a2e83a-884a-5379-baa8-16f193a13b21}",
    "hidden": false,
    "name": "PowerShell 7 Preview",
    "source": "Windows.Terminal.PowershellCore"
    }
    ]
    },
    "schemes": [
    {
    "background": "#0C0C0C",
    "black": "#0C0C0C",
    "blue": "#0037DA",
    "brightBlack": "#767676",
    "brightBlue": "#3B78FF",
    "brightCyan": "#61D6D6",
    "brightGreen": "#16C60C",
    "brightPurple": "#B4009E",
    "brightRed": "#E74856",
    "brightWhite": "#F2F2F2",
    "brightYellow": "#F9F1A5",
    "cursorColor": "#FFFFFF",
    "cyan": "#3A96DD",
    "foreground": "#CCCCCC",
    "green": "#13A10E",
    "name": "Campbell",
    "purple": "#881798",
    "red": "#C50F1F",
    "selectionBackground": "#FFFFFF",
    "white": "#CCCCCC",
    "yellow": "#C19C00"
    },
    {
    "background": "#012456",
    "black": "#0C0C0C",
    "blue": "#0037DA",
    "brightBlack": "#767676",
    "brightBlue": "#3B78FF",
    "brightCyan": "#61D6D6",
    "brightGreen": "#16C60C",
    "brightPurple": "#B4009E",
    "brightRed": "#E74856",
    "brightWhite": "#F2F2F2",
    "brightYellow": "#F9F1A5",
    "cursorColor": "#FFFFFF",
    "cyan": "#3A96DD",
    "foreground": "#CCCCCC",
    "green": "#13A10E",
    "name": "Campbell Powershell",
    "purple": "#881798",
    "red": "#C50F1F",
    "selectionBackground": "#FFFFFF",
    "white": "#CCCCCC",
    "yellow": "#C19C00"
    },
    {
    "background": "#283033",
    "black": "#000000",
    "blue": "#6666E9",
    "brightBlack": "#666666",
    "brightBlue": "#0000FF",
    "brightCyan": "#00E5E5",
    "brightGreen": "#00D900",
    "brightPurple": "#E500E5",
    "brightRed": "#E50000",
    "brightWhite": "#E5E5E5",
    "brightYellow": "#E5E500",
    "cursorColor": "#FFFFFF",
    "cyan": "#00A6B2",
    "foreground": "#00FF00",
    "green": "#00A600",
    "name": "Homebrew",
    "purple": "#B200B2",
    "red": "#FC5275",
    "selectionBackground": "#FFFFFF",
    "white": "#BFBFBF",
    "yellow": "#999900"
    },
    {
    "background": "#282C34",
    "black": "#282C34",
    "blue": "#61AFEF",
    "brightBlack": "#5A6374",
    "brightBlue": "#61AFEF",
    "brightCyan": "#56B6C2",
    "brightGreen": "#98C379",
    "brightPurple": "#C678DD",
    "brightRed": "#E06C75",
    "brightWhite": "#DCDFE4",
    "brightYellow": "#E5C07B",
    "cursorColor": "#FFFFFF",
    "cyan": "#56B6C2",
    "foreground": "#DCDFE4",
    "green": "#98C379",
    "name": "One Half Dark",
    "purple": "#C678DD",
    "red": "#E06C75",
    "selectionBackground": "#FFFFFF",
    "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",
    "cursorColor": "#4F525D",
    "cyan": "#0997B3",
    "foreground": "#383A42",
    "green": "#50A14F",
    "name": "One Half Light",
    "purple": "#A626A4",
    "red": "#E45649",
    "selectionBackground": "#FFFFFF",
    "white": "#FAFAFA",
    "yellow": "#C18301"
    },
    {
    "background": "#002B36",
    "black": "#002B36",
    "blue": "#268BD2",
    "brightBlack": "#073642",
    "brightBlue": "#839496",
    "brightCyan": "#93A1A1",
    "brightGreen": "#586E75",
    "brightPurple": "#6C71C4",
    "brightRed": "#CB4B16",
    "brightWhite": "#FDF6E3",
    "brightYellow": "#657B83",
    "cursorColor": "#FFFFFF",
    "cyan": "#2AA198",
    "foreground": "#839496",
    "green": "#859900",
    "name": "Solarized Dark",
    "purple": "#D33682",
    "red": "#DC322F",
    "selectionBackground": "#FFFFFF",
    "white": "#EEE8D5",
    "yellow": "#B58900"
    },
    {
    "background": "#FDF6E3",
    "black": "#002B36",
    "blue": "#268BD2",
    "brightBlack": "#073642",
    "brightBlue": "#839496",
    "brightCyan": "#93A1A1",
    "brightGreen": "#586E75",
    "brightPurple": "#6C71C4",
    "brightRed": "#CB4B16",
    "brightWhite": "#FDF6E3",
    "brightYellow": "#657B83",
    "cursorColor": "#002B36",
    "cyan": "#2AA198",
    "foreground": "#657B83",
    "green": "#859900",
    "name": "Solarized Light",
    "purple": "#D33682",
    "red": "#DC322F",
    "selectionBackground": "#FFFFFF",
    "white": "#EEE8D5",
    "yellow": "#B58900"
    },
    {
    "background": "#000000",
    "black": "#000000",
    "blue": "#3465A4",
    "brightBlack": "#555753",
    "brightBlue": "#729FCF",
    "brightCyan": "#34E2E2",
    "brightGreen": "#8AE234",
    "brightPurple": "#AD7FA8",
    "brightRed": "#EF2929",
    "brightWhite": "#EEEEEC",
    "brightYellow": "#FCE94F",
    "cursorColor": "#FFFFFF",
    "cyan": "#06989A",
    "foreground": "#D3D7CF",
    "green": "#4E9A06",
    "name": "Tango Dark",
    "purple": "#75507B",
    "red": "#CC0000",
    "selectionBackground": "#FFFFFF",
    "white": "#D3D7CF",
    "yellow": "#C4A000"
    },
    {
    "background": "#FFFFFF",
    "black": "#000000",
    "blue": "#3465A4",
    "brightBlack": "#555753",
    "brightBlue": "#729FCF",
    "brightCyan": "#34E2E2",
    "brightGreen": "#8AE234",
    "brightPurple": "#AD7FA8",
    "brightRed": "#EF2929",
    "brightWhite": "#EEEEEC",
    "brightYellow": "#FCE94F",
    "cursorColor": "#000000",
    "cyan": "#06989A",
    "foreground": "#555753",
    "green": "#4E9A06",
    "name": "Tango Light",
    "purple": "#75507B",
    "red": "#CC0000",
    "selectionBackground": "#FFFFFF",
    "white": "#D3D7CF",
    "yellow": "#C4A000"
    },
    {
    "background": "#000000",
    "black": "#000000",
    "blue": "#000080",
    "brightBlack": "#808080",
    "brightBlue": "#0000FF",
    "brightCyan": "#00FFFF",
    "brightGreen": "#00FF00",
    "brightPurple": "#FF00FF",
    "brightRed": "#FF0000",
    "brightWhite": "#FFFFFF",
    "brightYellow": "#FFFF00",
    "cursorColor": "#FFFFFF",
    "cyan": "#008080",
    "foreground": "#C0C0C0",
    "green": "#008000",
    "name": "Vintage",
    "purple": "#800080",
    "red": "#800000",
    "selectionBackground": "#FFFFFF",
    "white": "#C0C0C0",
    "yellow": "#808000"
    },
    {
    "background": "#323232",
    "black": "#323232",
    "blue": "#4099FF",
    "brightBlack": "#535353",
    "brightBlue": "#5EB7F7",
    "brightCyan": "#DCF4FF",
    "brightGreen": "#9DFF91",
    "brightPurple": "#FF9DFF",
    "brightRed": "#F07070",
    "brightWhite": "#FFFFFF",
    "brightYellow": "#FFE48B",
    "cursorColor": "#FFFFFF",
    "cyan": "#BED6FF",
    "foreground": "#00FF00",
    "green": "#7FE173",
    "name": "idleToes",
    "purple": "#F680FF",
    "red": "#D25252",
    "selectionBackground": "#FFFFFF",
    "white": "#EEEEEC",
    "yellow": "#FFC66D"
    }
    ]
    }
    • defaultProfile

      defaultProfile改为新的 powershell。

      1
      "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    • list

      在 list 中新增一个配置,也就是我们的新版 powershell。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      {
      // Powershell 7.1.0-preview.2 配置
      "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
      "hidden": false,
      "name": "pwsh",
      // 注意:一定要写上 -nologo,否则开启 powershll 会有一段话输出,很讨厌!
      "commandline": "C:/Program Files/PowerShell/7/pwsh.exe -nologo",
      "source": "Windows.Terminal.PowershellCore",
      // 启动菜单一定要设置为 <.>,否则后面重要的一步将会无效!
      "startingDirectory": ".",
      // 字体
      "fontFace": "Fira Code",
      "fontSize": 11,
      "historySize": 9001,
      "padding": "5, 5, 20, 25",
      "snapOnInput": true,
      "useAcrylic": false,
      // 颜色
      "colorScheme": "idleToes"
      },
    • colorScheme

      这个也就是配色。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      {
      "name": "idleToes",
      "black": "#323232",
      "red": "#d25252",
      "green": "#7fe173",
      "yellow": "#ffc66d",
      "blue": "#4099ff",
      "purple": "#f680ff",
      "cyan": "#bed6ff",
      "white": "#eeeeec",
      "brightBlack": "#535353",
      "brightRed": "#f07070",
      "brightGreen": "#9dff91",
      "brightYellow": "#ffe48b",
      "brightBlue": "#5eb7f7",
      "brightPurple": "#ff9dff",
      "brightCyan": "#dcf4ff",
      "brightWhite": "#ffffff",
      "background": "#323232",
      "foreground": "#00ff00"
      },

  7. 配置鼠标右键

    新版的 Terminal 安装后右键默认带有了菜单,无需在手动配置。

Tabby

配置完成 Power Shell7 core 之后可以直接在 Tabby 中使用。如果没有的话可以自己添加配置。

image-20220416224359121

问题说明

  1. 实验环境

    VMware 下的 Windows2004 版本

  2. 关于 Emoji 表情

    可以使用字体:JetBrainsMono Nerd Font Mono