自定义 Windows Terminal 终端界面与配色方案

本文发布于:2021-03-12,最后更新于:2023-01-30,如果内容失效请留言告知。
目录
[隐藏]

什么是 Windows Terminal?官方简介如下:

Windows 终端是一个面向命令行工具和 shell(如命令提示符、PowerShell 和适用于 Linux 的 Windows 子系统 (WSL))用户的新式终端应用程序。 它的主要功能包括多个选项卡、窗格、Unicode 和 UTF-8 字符支持、GPU 加速文本呈现引擎,你还可用它来创建你自己的主题并自定义文本、颜色、背景和快捷方式。

用上了 Windows Terminal,或许你就不再需要 cmder 之类的工具了。本文主要介绍在 windows 10 系统下 Windows Terminal 安装与主题配色相关的配置方式参考。

1. 安装 Windows Terminal

你可以在 windows 10 的应用商店中搜索并安装。也可以从 github 中下载最新的版本安装或更新:

  • https://github.com/microsoft/terminal/releases

自定义 Powershell 终端提示符:安装和配置 Powerline

Powerline 提供自定义的命令提示符体验,提供 Git 状态颜色编码和提示符。在官方文档有详细的介绍,这里仅提供快捷的操作步骤参考:

1.1 下载并安装 cascadia-code 字体

从如下地址下载 microsoft/cascadia-code 字体并安装:

https://github.com/microsoft/cascadia-code/releases

将下载到的 zip 压缩包解压,可以选择 otf 目录下的字体,双击打开各字体文件然后点击“安装”按钮执行安装。

1.2 在 PowerShell 中设置 Powerline

首先需要确保已经安装了 Git 客户端。下载并安装 Git 客户端:https://git-scm.com/downloads

然后从 PowerShell 终端中执行如下命令,安装相关依赖工具模块:

1
2
3
4
5
6
7
8
9
10
11
# 安装 Posh-Git
# Install-Module posh-git -Scope CurrentUser
Install-Module posh-git -Scope AllUsers -Force
 
# 安装 Oh-My-Posh
# Install-Module oh-my-posh -Scope CurrentUser
Install-Module oh-my-posh -Scope AllUsers -Force
 
# 安装 PSReadline
# Install-Module -Name PSReadLine -Scope CurrentUser -Force -SkipPublisherCheck
Install-Module -Name PSReadLine -Scope AllUsers -Force -SkipPublisherCheck

PowerShell 终端中执行如下命令,编辑或创建 PowerShell 配置文件:

1
notepad $PROFILE

在配置文件末尾添加如下内容,以便在 PowerShell 启动时加载相关工具模块和设置:

1
2
3
Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme Paradox

Windows Terminal终端配置文件中,设置 PowerShell 的字体为 Cascadia Code PL。示例:

1
2
3
4
5
6
7
8
9
10
11
12
{
    "profiles": {
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "fontFace": "Cascadia Code PL"
           },
        }
    }
}

2. 设置默认启动目录

进入 shell 默认的目录为当前登录用户主目录 %USERPROFILE%。可以通过设置 startingDirectory 参数自定义它。示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
    "profiles": {
        "defaults": {
            // 指定所有终端的默认启动目录
            "startingDirectory": "D:\\coding\\work" // "%USERPROFILE%"
        },
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "fontFace": "Cascadia Code PL",
               // 指定当前终端的默认启动目录
               "startingDirectory": "D:\\coding\\work",
           },
        }
    }
}

提示:在 defaults 中的设置是公共的配置,会被 list 所有终端类型所应用。

3. 为指定的 shell 环境设置背景图片

在对应的 shell 配置中添加 backgroundImagebackgroundImageOpacitybackgroundImageAlignment 等参数配置即可。示例参考如下:

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
{
    "profiles": {
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "colorScheme": "lzwmeThemeColor",
               "backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\<pre class="brush: jscript; title: ; notranslate" title="">{
"profiles": {
"list": {
{
"guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"colorScheme": "lzwmeThemeColor",
"backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\\000706.jpg",
"backgroundImageOpacity": 0.25,
"backgroundImageAlignment": "center",
"useAcrylic": true,
"acrylicOpacity" : 0.7,
"fontSize": 16,
"hidden": false
},
}
}
}
</pre>0706.jpg",
               "backgroundImageOpacity": 0.25,
               "backgroundImageAlignment": "center",
               "useAcrylic": true,
               "acrylicOpacity" : 0.7,
               "fontSize": 16,
               "hidden": false
           },
        }
    }
}

4. 自定义主题配色方案

打开 Windows Terminal 的配置文件,在 schemes 下添加配色方案,例如设置其 name 值为 lzwmeThemeColor,然后修改 PowerShell 的配置,添加 colorScheme": "lzwmeThemeColor", 指定使用 lzwmeThemeColor 配色方案。主要配置代码示例如下:

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
{
    "schemes": [
        {
            "name": "lzwmeThemeColor",
            "black": "#151515",
            "red": "#ac4142",
            "green": "#0eeb7d",
            "yellow": "#f3a62a",
            "blue": "#6c99bb",
            "purple": "#9f4e85",
            "cyan": "#7dd6cf",
            "white": "#d0d0d0",
            "brightBlack": "#505050",
            "brightRed": "#ac4142",
            "brightGreen": "#7e8e50",
            "brightYellow": "#e5b567",
            "brightBlue": "#6c99bb",
            "brightPurple": "#9f4e85",
            "brightCyan": "#7dd6cf",
            "brightWhite": "#f5f5f5",
            "background": "#212121",
            "foreground": "#d0d0d0"
        }
    ],
    "profiles": {
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "colorScheme": "lzwmeThemeColor",
               "hidden": false
           },
        }
    }
}

从上面你的示例可以看到,自定义配色方案主要就是在 schemes 下定义各预定义颜色的具体值。Windows Terminal Themes 这个网站提供了多达 228+ 种不同配色方案,如有兴趣可以自行体验和选用:相中了你喜欢的配色方案后,点击 Get theme 按钮,即可将配色方案复制到剪切板中。

5. 自定义 shell: 新增 Git bash 终端类型

list 字段中可以增加配置任意可启动的终端类型,只需要通过 commandline 指定启动的可执行程序,再辅以其他相关的配置信息即可。

下面为增加一个 Git bash 终端类型的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
    "profiles": {
        "list": {
            {
                "name": "Git Bash",
                "guid": "{eeeee-4e3d-5e58-b989-0a998ec4abcd}",
                "closeOnExit": true,
                "colorScheme": "lzwmeThemeColor", // Campbell
                "commandline": "D:\\Program Files\\Git\\bin\\bash.exe",
                "icon": "D:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico",
                "historySize": 9999,
                "padding": "0, 0, 0, 0",
                "snapOnInput": true,
                "fontSize": 18,
                "hidden": false
           },
        }
    }
}

注意 guid 的取值,应为 a-f0-9 之间的字符。

6. 总结与完整示例

以上便完成了对 Windows Terminal 基本的个性化配置。其中各主题设置、配色方案等均可按你自己喜欢的方式进行设置。

一个完整的配置参考:

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
{
    "defaultProfile": "{aaaaaaaa-c2c6-5271-96e7-009a87ff44bf}",
    "copyOnSelect": true,
    "copyFormatting": true,
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
    "profiles": {
        "defaults": {
            "startingDirectory": "D:\\coding\\work", // "%USERPROFILE%"
            "cursorShape": "bar",
            "fontFace": "Consolas",
            "backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\<pre class="brush: jscript; title: ; notranslate" title="">{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{aaaaaaaa-c2c6-5271-96e7-009a87ff44bf}",
"copyOnSelect": true,
"copyFormatting": true,
// To learn more about profiles, visit https://aka.ms/terminal-profile-settings
"profiles": {
"defaults": {
"startingDirectory": "D:\\coding\\work", // "%USERPROFILE%"
"cursorShape": "bar",
"fontFace": "Consolas",
"backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\\000706.jpg",
"backgroundImageOpacity": 0.35
},
"list":
[
{
"guid": "{aaaaaaaa-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
// "source" : "Windows.Terminal.PowershellCore",
"backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\\ysdhwexx.jpg",
"backgroundImageOpacity": 0.15,
"backgroundImageAlignment": "center",
"useAcrylic": true,
"acrylicOpacity" : 0.7,
"colorScheme" : "lzwmeThemeColor",
"cursorColor" : "#FFFFFD",
"startingDirectory": "D:\\coding\\work",
"fontSize": 16,
"fontFace" : "Cascadia Code PL",
"hidden": false
},
{
"guid": "{aaaaaaaa-35be-5f56-a8ff-afceeeaa6101}",
"name": "命令提示符",
"commandline": "cmd.exe",
"hidden": false
},
{
"name": "Git Bash",
"guid": "{aaaaaaaa-4e3d-5e58-b989-0a998ec4abcd}",
"closeOnExit": true,
"colorScheme": "lzwmeThemeColor", // Campbell
"commandline": "D:\\Program Files\\Git\\bin\\bash.exe",
"icon": "D:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico",
"historySize": 9999,
"padding": "0, 0, 0, 0",
"snapOnInput": true,
"fontSize": 18,
"hidden": false
},
{
"guid": "{aaaaaaaa-4e3d-5e58-b989-0a998ec441b8}",
"hidden": true,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
}
]
},
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
"name": "lzwmeThemeColor",
"black": "#151515",
"red": "#ac4142",
"green": "#0eeb7d",
"yellow": "#f3a62a",
"blue": "#6c99bb",
"purple": "#9f4e85",
"cyan": "#7dd6cf",
"white": "#d0d0d0",
"brightBlack": "#505050",
"brightRed": "#ac4142",
"brightGreen": "#7e8e50",
"brightYellow": "#e5b567",
"brightBlue": "#6c99bb",
"brightPurple": "#9f4e85",
"brightCyan": "#7dd6cf",
"brightWhite": "#f5f5f5",
"background": "#212121",
"foreground": "#d0d0d0"
}
],
"actions": [
{ "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" },
{ "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" },
{ "command": { "action": "newTab", "index": 0 }, "keys": "ctrl+n" },
{ "command": "closePane", "keys": "ctrl+w" }
]
}
</pre>0706.jpg",
            "backgroundImageOpacity": 0.35
        },
        "list":
        [
            {
                "guid": "{aaaaaaaa-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                // "source" : "Windows.Terminal.PowershellCore",
                "backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\\ysdhwexx.jpg",
                "backgroundImageOpacity": 0.15,
                "backgroundImageAlignment": "center",
                "useAcrylic": true,
                "acrylicOpacity" : 0.7,
                "colorScheme" : "lzwmeThemeColor",
                "cursorColor" : "#FFFFFD",
                "startingDirectory": "D:\\coding\\work",
                "fontSize": 16,
                "fontFace" : "Cascadia Code PL",
                "hidden": false
            },
            {
                "guid": "{aaaaaaaa-35be-5f56-a8ff-afceeeaa6101}",
                "name": "命令提示符",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "name": "Git Bash",
                "guid": "{aaaaaaaa-4e3d-5e58-b989-0a998ec4abcd}",
                "closeOnExit": true,
                "colorScheme": "lzwmeThemeColor", // Campbell
                "commandline": "D:\\Program Files\\Git\\bin\\bash.exe",
                "icon": "D:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico",
                "historySize": 9999,
                "padding": "0, 0, 0, 0",
                "snapOnInput": true,
                "fontSize": 18,
                "hidden": false
            },
            {
                "guid": "{aaaaaaaa-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": true,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            }
        ]
    },
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [
        {
            "name": "lzwmeThemeColor",
            "black": "#151515",
            "red": "#ac4142",
            "green": "#0eeb7d",
            "yellow": "#f3a62a",
            "blue": "#6c99bb",
            "purple": "#9f4e85",
            "cyan": "#7dd6cf",
            "white": "#d0d0d0",
            "brightBlack": "#505050",
            "brightRed": "#ac4142",
            "brightGreen": "#7e8e50",
            "brightYellow": "#e5b567",
            "brightBlue": "#6c99bb",
            "brightPurple": "#9f4e85",
            "brightCyan": "#7dd6cf",
            "brightWhite": "#f5f5f5",
            "background": "#212121",
            "foreground": "#d0d0d0"
        }
    ],
    "actions": [
        { "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" },
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" },
        { "command": { "action": "newTab", "index": 0 }, "keys": "ctrl+n" },
        { "command": "closePane", "keys": "ctrl+w" }
    ]
}

效果图参考:

相关参考

  • https://github.com/microsoft/terminal/releases/
  • https://docs.microsoft.com/zh-cn/windows/terminal/custom-terminal-gallery/custom-schemes
  • https://docs.microsoft.com/zh-cn/windows/terminal/tutorials/powerline-setup
  • https://ohmyposh.dev/docs/configure/
点赞 (3)
  1. 王光卫博客说道:
    Google Chrome 114.0.0.0 Google Chrome 114.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7

    居然还能这样操作,有没有Mac的教程呢 :razz:

  2. 说道:
    Google Chrome 96.0.4664.45 Google Chrome 96.0.4664.45 Windows 10 x64 Edition Windows 10 x64 Edition

    你倒是把背景图也放出来啊,要无水印高清版的。

  3. Jitian说道:
    Google Chrome 91.0.4472.114 Google Chrome 91.0.4472.114 GNU/Linux x64 GNU/Linux x64

    大佬,水平挺高的 :razz:

  4. 流量卡说道:
    Google Chrome 86.0.4240.198 Google Chrome 86.0.4240.198 Windows 7 x64 Edition Windows 7 x64 Edition

    这妹子蛮漂亮的

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code