Talk is cheap, Show me the code

0%

Windows Terminal 美化,打造个性化终端

记录Windows Terminal 改造过程,基于oh-my-posh主题

Windows Terminal 美化,打造个性化终端

一个称手好用还好看的终端是保证工作效率的关键!

以前用Cmder,界面简约功能也不少,但后来遇到一些bug,多次尝试解决无果,最终只得另外寻找新的工具。

逛一圈下来最终又回到了Windows Terminal。

不过,原版用着显然是不爽的,又丑信息显示又少,于是加点主题改造一番,下面进入正题。

1. 安装Windows Terminal和Oh-my-Posh

1.1 安装Windows Terminal

  • 直接去应用商店搜索“Windows Terminal“安装即可

1.2 安装Oh-my-Posh和posh-git

  • 最新安装方式请参考官方文档Oh my Posh 官方文档
  • 以管理员方式打开Windows Terminal,依次运行以下命令
1
2
3
4
5
6
7
8
# 绕过power shell执行策略,使其可以执行脚本文件
Set-ExecutionPolicy Bypass
# 安装oh-my-posh,仅为当前用户
Install-Module oh-my-posh -Scope CurrentUser
# 安装posh-git,用于显示git信息,仅为当前用户
Install-Module posh-git -Scope CurrentUser

# 中途若有询问,选择Y

2. 选择并配置默认主题

2.1 安装和使用字体

  • 安装字体

    Oh-my-Posh使用了很多个性化的图标,需要合适的字体才能比较好地适配

    字体下载地址:Nerd Fonts

    官方推荐 Meslo LGM NF,本文使用Cousine Nerd Font,可根据需要进行选择并安装

  • 使用字体

在Windows Terminal中,上方标签页找到设置

设置页中左边配置文件栏选择Windows PowerShell,右边设置中进入外观选项

选项中选择字体,然后保存即可,未生效则重启Windows Terminal

2.2 查看和设置主题

  • 官网主题预览Themes
  • 预览所有主题
1
Get-PoshThemes
  • 设置主题
1
Set-PoshPrompt -Theme agnoster

2.3 设置默认主题

  • 创建脚本配置文件
1
2
3
4
# 启动编辑power shell配置文件的引擎
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
# 用记事本打开配置文件
notepad $PROFILE
  • 在脚本配置文件中填入以下内容,即可设置默认主题
1
Set-PoshPrompt -Theme agnoster

3. 配置第三方信息显示

3.1 显示python虚拟环境

  • 官方文档Python
  • 初级:随时显示conda环境
1
2
# 在powershell中运行下述命令,然后再选一个配置有虚拟环境显示的主题即可
conda init powershell
  • 进阶:仅需要时加载conda,提高终端启动速度
1
2
3
4
5
6
7
8
9
10
# 0.初级方案启动新terminal时都会加载conda,速度极慢,因此仅在需要时加载conda,方案如下

# 1.修改conda脚本文件名:将初级中生成的profile.ps1(与$PROFILE位于同一目录)文件名修改为任意名字如condaInit.ps1
# 2.修改$PROFILE,为conda初始化脚本调用设置别名:
notepad $PROFILE # 打开文件
Set-Alias condaInit "C:\Users\XXX\Documents\Powershell\condaInit.ps1" # 添加这一行,路径改为自己的路径,“condaInit”即为初始化conda的别名
# 3. 修改conda脚本,在最后添加设置主题的操作:
oh-my-posh init pwsh --config THEME | Invoke-Expression # 添加到最后,这行与$PROFILE设置默认主题的命令一致,作用是修复加载conda引起的格式错误

# 完成设置后,开启速度<1s,在终端内运行`condaInit`即可加载conda环境
  • 下面是我的两个配置文件示例

    • condaInit.ps1
    1
    2
    3
    4
    5
    6
    #region conda initialize
    # !! Contents within this block are managed by 'conda init' !!
    (& "C:\Users\BrightHammer\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
    #endregion

    oh-my-posh init pwsh --config $default_theme | Invoke-Expression
    • Microsoft.PowerShell_profile.ps1
    1
    2
    3
    $default_theme="~\.omp-theme\.mytheme.json"
    oh-my-posh init pwsh --config $default_theme | Invoke-Expression
    Set-Alias condaInit "C:\Users\BrightHammer\Documents\Powershell\condaInit.ps1"

4. 配置自定义主题

  • 现有主题无法完美满足自己需求,就需要修改主题配置文件来自定义了,官网已经给出了详细的主题配置规则
  • 主题配置规则Configuration

4.1 导出主题配置文件

  • 建议基于原有主题文件进行修改
1
2
3
4
# 导出当前主题以便修改
Export-PoshTheme -FilePath ~/.oh-my-posh.omp.json
# 设置自定义的主题
Set-PoshPrompt -Theme ~/.oh-my-posh.omp.json

也可以去官网下载对应的主题文件: 主题配置文件下载

4.2 定制主题

  • 接着修改主题配置文件,有想加的功能可以到其他有该功能的主题中找如何实现,终极缝合怪
  • 效果图

  • 完整配置文件
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
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"segments": [
{
"background": "#1d1d1d",
"foreground": "#26C6DA",
"properties": {
"alpine": "\uf300",
"arch": "\uf303",
"centos": "\uf304",
"debian": "\uf306",
"elementary": "\uf309",
"fedora": "\uf30a",
"gentoo": "\uf30d",
"linux": "\ue712",
"macos": "\ue711",
"manjaro": "\uf312",
"mint": "\uf30f",
"opensuse": "\uf314",
"raspbian": "\uf315",
"template": "{{ if .WSL }}\ue712 on {{ end }}{{ .Icon }}<#7a7a7a> </>",
"ubuntu": "\uf31c",
"windows": "\ue70f"
},
"style": "diamond",
"type": "os"
},
{
"background": "#1d1d1d",
"foreground": "#81A1C1",
"properties": {
"style": "full",
"template": "{{ .Path }} "
},
"style": "plain",
"type": "path"
},
{
"background": "#1d1d1d",
"foreground": "#C591E8",
"leading_diamond": " \ue0b6",
"properties": {
"fetch_version": false,
"template": "\ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ end }}"
},
"style": "plain",
"type": "python"
}
],
"type": "prompt"
},
{
"alignment": "left",
"segments": [
{
"background": "#1d1d1d",
"foreground": "#9b9b9b",
"properties": {
"branch_ahead_icon": "<#88C0D0>\u21e1 </>",
"branch_behind_icon": "<#88C0D0>\u21e3 </>",
"fetch_stash_count": true,
"fetch_status": true,
"fetch_upstream_icon": true,
"github_icon": "",
"template": " {{ .UpstreamIcon }}{{ .HEAD }}{{ .BranchStatus }}{{ 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 }} \uf692 {{ .StashCount }}{{ end }} "
},
"style": "plain",
"type": "git"
}
],
"type": "prompt"
},

{
"alignment": "left",
"segments": [
{
"foreground": "#A3BE8C",
"properties": {
"style": "austin",
"template": " {{ .FormattedMs }} "
},
"style": "plain",
"type": "executiontime"
}
],
"type": "prompt"
},

{
"alignment": "right",
"type": "prompt",
"segments": [
{
"type": "time",
"style": "plain",
"foreground": "#ffffff",
"properties": {
"time_format": "[15:04:05]"
}
}
]
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#01a15e",
"foreground_templates": [
"{{ if gt .Code 0 }}#BF616A{{ end }}"
],
"properties": {
"always_enabled": true,
"template": "\u276f "
},
"style": "plain",
"type": "exit"
}
],
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}(Admin){{end}} {{.PWD}}",
"version": 2
}

参考

-------------The End-------------