Windows Terminal 美化

🎉 powershell7.1.2,oh-my-posh3

本文包含 Powershell7 以及 Windows Terminal 的美化。

写在前面

注意

本文现使用 oh-my-posh3 美化 powershell,但本文的历史版本基于 oh-my-posh2,可在 github 上参考本文的历史版本。

效果图

PowerShell7 美化

安装 oh-my-posh3

1
scoop install oh-my-posh3

新建或打开 powershell 的配置文件 $profile

执行 notepad $profilecode $profilesubl $profile 编辑配置文件

向其中添加以下内容,其中第 15 行( oh-my-posh.exe )和 17 行(设置主题)需要修改成正确的路径。

 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
[ScriptBlock]$Prompt = {
    $lastCommandSuccess = $?
    $realLASTEXITCODE = $global:LASTEXITCODE
    $errorCode = 0
    if ($lastCommandSuccess -eq $false) {
        #native app exit code
        if ($realLASTEXITCODE -is [int]) {
            $errorCode = $realLASTEXITCODE
        }
        else {
            $errorCode = 1
        }
    }
    $startInfo = New-Object System.Diagnostics.ProcessStartInfo
    $startInfo.FileName = "C:\Files\scoop\apps\oh-my-posh3\current\bin\oh-my-posh.exe"
    $cleanPWD = $PWD.ProviderPath.TrimEnd("\")
    $startInfo.Arguments = "-config=""C:\Files\scoop\apps\oh-my-posh3\current\themes\zash.omp.json"" -error=$errorCode -pwd=""$cleanPWD"""
    $startInfo.Environment["TERM"] = "xterm-256color"
    $startInfo.CreateNoWindow = $true
    $startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
    $startInfo.RedirectStandardOutput = $true
    $startInfo.UseShellExecute = $false
    if ($PWD.Provider.Name -eq 'FileSystem') {
      $startInfo.WorkingDirectory = $PWD.ProviderPath
    }
    $process = New-Object System.Diagnostics.Process
    $process.StartInfo = $startInfo
    $process.Start() | Out-Null
    $standardOut = $process.StandardOutput.ReadToEnd()
    $process.WaitForExit()
    $standardOut
    $global:LASTEXITCODE = $realLASTEXITCODE
    #remove temp variables
    Remove-Variable realLASTEXITCODE -Confirm:$false
    Remove-Variable lastCommandSuccess -Confirm:$false
}
Set-Item -Path Function:prompt -Value $Prompt -Force

所有主题的 json 文件都在 oh-my-posh3 安装目录中的 themes 文件夹中。

美化 ls 命令

使用 Get-ChildItem 美化 ls 命令,首先需要安装,在 powershell 执行以下命令(没有 sudo 就用管理员方式运行)

1
sudo Install-Module -AllowClobber Get-ChildItemColor

执行 code $profile,将下列内容追加进去

1
2
3
Import-Module Get-ChildItemColor
Set-Alias ll Get-ChildItem -option AllScope  # ll
Set-Alias ls Get-ChildItemColorFormatWide -option AllScope  # ls

可以在 terminal 中尝试一下 lsll 命令。

Windows Terminal 美化

打开 Windows Terminal 的 Settings,即 settings.json 文件。

下面我的配置可供参考。

其中 guid 可以在 powershell 中用命令生成 [Guid]::NewGuid() 生成。

  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
// This file was initially generated by Windows Terminal 1.3.2651.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{d26dae7e-9f65-4c0b-982f-21dca5115e77}",

    // You can add more global application settings here.
    // To learn more about global settings, visit https://aka.ms/terminal-global-settings

    // If enabled, selections are automatically copied to your clipboard.
    "copyOnSelect": false,
    
    // If enabled, formatted data is also copied to your clipboard
    "copyFormatting": false,

    // A profile specifies a command to execute paired with information about how it should look and feel.
    // Each one of them will appear in the 'New Tab' dropdown,
    //   and can be invoked from the commandline with `wt.exe -p xxx`
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings

    "initialCols": 110,           //终端窗口初始宽度
    "initialRows": 30,            //终端窗口初始高度

    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
            "fontFace": "JetBrainsMono NF",  // 等距更纱黑体 SC / Courier Prime
            "fontSize": 13,
            "fontWeight": "normal",
            "cursorShape": "vintage",
            "colorScheme": "gugugu",
            "useAcrylic": true,
            "acrylicOpacity": 0.9,
            "cursorColor": "#7975e6",
            "experimental.retroTerminalEffect": false  // 有趣
        },
        "list":
        [   
            {
                "guid": "{d26dae7e-9f65-4c0b-982f-21dca5115e77}",
                "name": "PowerShell7",
                "commandline": "C:/Files/pwsh/pwsh.exe -WorkingDirectory ~",
                "icon": "C:/Users/yuhixyz/Pictures/terminal/pwsh.png",
                "hidden": false
            },
            {
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "PowerShell5",
                "commandline": "powershell.exe",
                "hidden": true
            },
            {
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "Command",
                "commandline": "cmd.exe",
                "hidden": true
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": true,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "guid": "{3e87d0d7-4025-451e-a03e-21952a86cd79}",
                "name": "Aliyun-yuhixyz",
                "commandline": "ssh root@yuhi.xyz -p 22",
                "icon": "C:/Users/yuhixyz/Pictures/terminal/aliyun.png",
                "hidden": false
            },
            {
                "guid": "{7eae62b5-4044-43ca-b4e7-3be3b09ad75f}",
                "name": "Tencent-yuhixyz",
                "commandline": "ssh -i ~/OneDrive/secret/surface root@b.yuhi.xyz -p 8972",
                "hidden": false,
                "icon": "C:/Users/yuhixyz/Pictures/terminal/tencent.png"
            }
        ]
    },

    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [
        {
            "name": "gugugu",
            "background": "#2e3440",
            "foreground": "#eceff4",
            "brightBlack":  "#3f68b9",
            "brightBlue": "#5e81ac",
            "brightCyan": "#8fbcbb",
            "brightGreen": "#a3be8c",
            "brightPurple": "#b48ead",
            "brightRed": "#bf616a",
            "brightWhite": "#eceff4",
            "brightYellow": "#ebcb8b",
            "black": "#2e3440",
            "blue": "#5e81ac",
            "cyan": "#8fbcbb",
            "green": "#a3be8c",
            "purple": "#b48ead",
            "red": "#bf616a",
            "white": "#eceff4",
            "yellow": "#ebcb8b"
        }
    ],

    // Add custom actions and keybindings to this array.
    // To unbind a key combination from your defaults.json, set the command to "unbound".
    // To learn more about actions and keybindings, visit https://aka.ms/terminal-keybindings
    "actions":
    [
        // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
        // These two lines additionally bind them to Ctrl+C and Ctrl+V.
        // To learn more about selection, visit https://aka.ms/terminal-selection
        { "command": { "action": "copy", "singleLine": false }, "keys": "ctrl+c" },
        { "command": "paste", "keys": "ctrl+v" },

        // Press Ctrl+Shift+F to open the search box
        { "command": "find", "keys": "ctrl+shift+f" },

        // Press Alt+Shift+D to open a new pane.
        // - "split": "auto" makes this pane open in the direction that provides the most surface area.
        // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
        // To learn more about panes, visit https://aka.ms/terminal-panes
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" },

        // 自定义
        { "command": "closeTab", "keys": "ctrl+w" },
        { "command": "newTab", "keys": "ctrl+q" },
        { "command": "closePane", "keys": "alt+shift+w" } // 默认为ctrl+shift+w
    ]
}

其中用到的字体可以使用 scoop 安装

1
sudo scoop install JetBrainsMono-NF

更详细的美化推荐看下面的参考资料。

参考资料

  1. Introduction | Oh my Posh
  2. 一份简单的 PowerShell 美化指南 - 知乎 (zhihu.com)
  3. Oh my Posh 3——易于自定义主题的Powershell美化工具 - 知乎 (zhihu.com)
updatedupdated2021-03-052021-03-05
更新文章:Windows Terminal 美化
点击刷新