Windows下可以使用oh-my-posh工具来美化powershell终端(注意不是cmd).

安装oh-my-posh

使用powershell输入如下命令来安装oh-my-posh到你的电脑:

1
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))

该命令将安装oh-my-posh.exe应用程序和配套的各种theme主题.

安装后重新启动终端.


然后powershell中输入oh-my-posh尝试启动该程序,如果未报错则安装成功:
image-20240218150959869

接下来就可以开始配置主题了.

配置字体

由于美化终端需要Nerd字体支持, 因此需要下载一款Nerd字体并安装.

在该链接https://www.nerdfonts.com/中下载字体, 选择自己喜欢的字体即可, 我选择的是这个:

image-20240218151302524

直接下载压缩包后,解压,里面是一大堆ttf后缀的字体文件, windows可以直接右键安装:

image-20240218151544693

我这里将所有下载的ttf文件批量安装(当然你可以选择仅安装自己想要的那个字体ttf文件):

按win键搜索font关键字,打开控制面板-字体界面:

image-20240218151651818

全选ttf文件,注意不要把LICENSE.txtREADME.md文件选择,直接拖入控制面板中:

image-20240218151916494 等待全部安装完成即可.
然后就可以正式开始字体和主题的选择了. # 选择字体

以管理员权限打开powershell,然后按ctrl+ shift + ,快捷键,会打开一个settings.json,在里面找到该段:

1
2
3
4
5
"profiles": 
{
"defaults": {},
其他代码...
}

在里面填写如下配置,此时就可以选择你安装的字体:

1
2
3
4
5
6
7
8
9
10
11
"profiles": 
{
"defaults":
{
"font":
{
"face": "MesloLGM Nerd Font"
}
},
其他代码...
}

比如上述我安装的是MesloLGM Nerd Font字体,在前面的控制面板中找到该字体的完整名称(必须严格一致,否则会powershell报错找不到该字体):
image-20240218152638650
修改完成后一定保存,然后再次检查刚刚的powershell是否报错和新字体是否成功应用,成功后即可关闭该json文件.

配置终端启动文件和主题

为了使用主题,我们要将对应的命令写入到powershell的启动配置文件中.
在powershell中运行该命令:

1
notepad $PROFILE

这样会以记事本打开终端启动配置文件.

如果上述命令报错,则需使用该命令确保先创建配置文件:

1
New-Item -Path $PROFILE -Type File -Force

然后再次尝试即可.


假设我们选择该主题:wholespace(我个人选择的一款主题),在打开的配置文件中(.ps1后缀的脚本文件)中写入如下内容:

1
oh-my-posh init pwsh --config 'C:\Users\admin\AppData\Local\Programs\oh-my-posh\themes\wholespace.omp.json' |  Invoke-Expression

该路径C:\Users\admin\AppData\Local\Programs\oh-my-posh\themes\是默认的主题配置文件保存路径.
保存后关闭记事本即可.

接下来尝试应用该配置:

1
. $PROFILE

如果终端成功发生变化则说明成功.


当然还可以选择其他喜欢的主题,使用该命令即可罗列出所有的主题名及其效果:

1
Get-PoshThemes

结果如下:

image-20240218154039275

找到自己喜欢的一款主题,例如图中的atomic,将该名称替换掉前面启动配置文件中的对应内容:

image-20240218154210844

改成这样:

1
oh-my-posh init pwsh --config 'C:\Users\admin\AppData\Local\Programs\oh-my-posh\themes\atomic.omp.json' |  Invoke-Expression

然后重新运行. $PROFILE即可.


完全可以在该目录下新建一个配置文件,进行自己的定制化.
配置文件的编写可以参考官方文档(见附注).
我这里基于wholespace主题做了一点魔改,效果如下:

image.png

配置文件内容如下,喜欢的朋友可以自取:

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
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"segments": [
{
"background": "#F4AD35",
"foreground": "#011627",
"leading_diamond": "\u256d\u2500\ue0b2",
"properties": {
"macos": "\uf179 ",
"ubuntu": "\uf31b ",
"windows": "\ue62a "
},
"style": "diamond",
"template": " {{ if .WSL }}WSL at {{ end }}{{.Icon}}",
"trailing_diamond": "<transparent,#F4AD35>\ue0b2</>",
"type": "os"
},
{
"background": "#ED7827",
"foreground": "#011627",
"leading_diamond": "\ue0b2",
"properties": {
"time_format": "15:04:05"
},
"style": "diamond",
"template": " \u2665 {{ .CurrentDate | date .Format }} ",
"trailing_diamond": "<transparent,#ED7827>\ue0b2</>",
"type": "time"
},
{
"background": "#59C9A5",
"foreground": "#ffffff",
"leading_diamond": "\ue0b2",
"style": "diamond",
"template": "\ue266 CPU: {{ round .PhysicalPercentUsed .Precision }}% | ",
"type": "sysinfo"
},
{
"background": "#59C9A5",
"foreground": "#ffffff",
"style": "diamond",
"template": "RAM: {{ (div ((sub .PhysicalTotalMemory .PhysicalFreeMemory)|float64) 1073741824.0) }}/{{ (div .PhysicalTotalMemory 1073741824.0) }}GB \ue266 ",
"trailing_diamond": "<transparent,#59C9A5>\ue0b2</>",
"type": "sysinfo"
},
{
"background": "#4B95E9",
"foreground": "#d6deeb",
"leading_diamond": "\ue0b2",
"properties": {
"style": "roundrock",
"threshold": 0
},
"style": "diamond",
"template": " {{ .FormattedMs }} ",
"trailing_diamond": "\ue0b0",
"type": "executiontime"
}
],
"type": "prompt"
},
{
"alignment": "right",
"segments": [
{
"background": "#ffffff",
"foreground": "#000000",
"leading_diamond": "\ue0b2",
"properties": {
"fetch_package_manager": true,
"npm_icon": " <#cc3a3a>\ue5fa</> ",
"yarn_icon": " <#348cba>\ue6a7</>"
},
"style": "diamond",
"template": "\ue718 {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }}",
"trailing_diamond": "<transparent,#ffffff>\ue0b2</>",
"type": "node"
},
{
"background": "#EE79D1",
"foreground": "#011627",
"leading_diamond": "\ue0b2",
"properties": {
"branch_icon": "\ue725 ",
"fetch_stash_count": true,
"fetch_status": true,
"fetch_upstream_icon": true,
"fetch_worktree_count": true
},
"style": "diamond",
"template": " {{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
"trailing_diamond": "\ue0b0",
"type": "git"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#F4AD35",
"style": "plain",
"template": "\u2502",
"type": "text"
},
{
"foreground": "#FFFFFF",
"style": "plain",
"template": " Hello WAHAHA! Look before you leap!",
"type": "text"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#F4AD35",
"style": "plain",
"template": "\u2570\u2500[",
"type": "text"
},
{
"foreground": "#ffafd2",
"properties": {
"folder_icon": "\uf07b",
"home_icon": "\b\b\uF108 home",
"style": "agnoster_full",
"folder_separator_icon": "/",
"mapped_locations": {
"D:\\Data\\code": "\b\b\uF121 D:/Data/code",
"D:\\Data\\CTF": "\b\b\uF21B D:/Data/CTF",
"D:\\Data\\\u6587\u6863": "\b\b\uF15C D:/Data/\u6587\u6863",
"E:\\": "\b\b\uF0AD E:/"
}
},
"style": "diamond",
"template": " \ue5ff {{ .Path }} ",
"type": "path"
},
{
"foreground": "#F4AD35",
"style": "plain",
"template": "] ",
"type": "text"
},
{
"foreground": "#17e528",
"foreground_templates": [
"{{ if gt .Code 0 }}#f00000{{ end }}"
],
"properties": {
"always_enabled": true
},
"style": "plain",
"template": "\uD83E\uDD23\ud83d\udc49 \uf155 ",
"type": "status"
}
],
"type": "prompt"
}
],
"console_title_template": "{{ .Folder }}",
"transient_prompt": {
"background": "transparent",
"foreground": "#FEF5ED",
"template": "-------------------------------------------------------------------\n> "
},
"version": 2
}

附注

参考: