oh-my-posh美化PowerShell

爱美之心,人皆有之

Windows下的shell真的是丑出天际,字体也非常别扭,之前用的cmder,刚开始相比cmd的话,确实是美观多了,不过使用了一段时间之后又觉得很丑了,而且cmder实现原理在于把一些常用的linux命令在windows下编译为可执行文件,这样就导致一定的命令延迟,这让我觉得莫名的别扭(好吧,我就是审美奇葩),在加上最近准备把powershell作为主力shell,而默认的PowerShell比CMD还要感人,需要进行一下美化

oh-my-posh是windows下的主题工具,借鉴于linux下的oh-my-zsh,我此次就是利用该工具来美化PS

安装包管理器

scoop是Windows下一款非常好用的包管理工具,能够自动设置环境变量,类似于linux下的apt等包管理工具,官网Click Here

1
2
Set-ExecutionPolicy RemoteSigned -scope CurrentUser # 设置powershell执行策略
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh') # 安装scoop

安装完成后,输入scoop,如下所示即说明安装成功

安装git与posh-git

1
2
3
scoop install -g git
scoop bucket add extras
scoop install posh-git

安装oh-my-posh

1
scoop install oh-my-posh

安装过程中如果出现超时错误的话请扶墙

安装字体

中文环境下,默认的ps字体不仅不美观,而且很多特殊字符也不支持,这会导致后面修改主题时看不到效果,出现乱码,而powershell的字体要求非常高,一般的字体无法在原生的ps终端使用,根据其它大佬的测试,Sarasa Gothic字体是符合ps的苛刻要求的,Github地址

安装powerline字体

除了上述字体之外,还可以安装powerline系列字体,能够很好的适配,Github地址在这
下载之后选择自己喜欢的字体安装,也可以运行install.ps1全部安装

启用oh-my-posh

接下来需要编写配置文件,才能够让配置每次自动生效,配置路径在$profile中,我们可以输入该变量查看配置文件位置

1
2
➜ $profile
C:\Users\Snow\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

配置文件内容如下

1
2
3
Import-Module DirColors
Import-Module Posh-git
Import-Module oh-my-posh

DirColors是一个对输出进行着色的模块

配置主题:
oh-my-posh内置多个主题,使用Set-Theme命令可以预览主题,TAB键可以切换不同的主题,我个人比较喜欢Honukai主题
在修改主题后,需要将改动写入配置文件$profile中,否则下次启动就还是原主题,在配置文件中加一行内容

1
Set-Theme Honukai

修改配置:

输入$ThemeSettings可以查看当前设置

修改用户名

1
$ThemeSettings.CurrentUser = 'aesm1p'

如果想要永久生效需要将上述命令写入配置文件$profile

第三方终端

在按照上述方法配置之后,PS就能够使用了。不过,虽然原生PS运行效率高,速度快,可由于PS太过底层,导致颜值控们进行美化修改时存在诸多限制,比如字体,上文提到的能在原生ps下使用的字体寥寥无几,选择空间有限,而且界面配色,背景等设置在PS中也非常困难,而通过第三方终端可以很方便的进行设置美化

Windows Terminal

个人最推荐的一款,微软爸爸官方开源,维护很及时,而且字体的锯齿消除做的是最好的,而且官方应用优化也是更加的独到,缺点是目前配置主要使用json文件来配置,没有gui界面,对小白不太友好,不过WindowsTerminal有完善的官方文档指导还是很贴心的。
安装该软件需要windows10版本为1903以上,可以通过Microsoft Store安装,也可以在github下载安装,通过store安装的话能够自动更新,手动安装无法自动获取更新。
安装完成之后进入软件打开设置settings.json,设置文件结构为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"theme": "light", //主题有light,dark,system三种
"profiles": //配置选项
{
"defaults":
{
//此处添加全局配置
},
"list":
[
//单独配置
]
},
"schemes": //配色方案,以列表形式存储
[
{},//scheme
],
"keybindings"://绑定快捷键,具体可修改的快捷键参数可以查阅官方文档
[
{},
]
}

了解清楚配置文件结构之后添加相应的配置参数即可,主要是字体大小,配色方案,键绑定等,这里贴上我的配置方案
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
// This file was initially generated by Windows 终端(未打包) 1.2.200825001-release1.2
// 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": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"theme": "light",

// 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
"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles.
"name":"PowerShell",
"source": "Windows.Terminal.PowershellCore",
"colorScheme": "One Half Dark",
"cursorColor":"#FFFFFF",
"fontFace":"Meslo LG M for Powerline",
"fontSize":10,
"cursorShape":"filledBox",
"startingDirectory":"your dir",
"backgroundImage" : "image path",
"backgroundImageOpacity": 0.3
},
"list":
[
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "命令提示符",
"commandline": "cmd.exe",
"hidden": false
},
// {
// "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
// "hidden": false,
// "name": "Azure Cloud Shell",
// "source": "Windows.Terminal.Azure"
// }
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure",
"source": "Windows.Terminal.Azure"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec44156}",
"name": "Python InterAction",
"commandline": "py",
"hidden": false
}
]
},

// Add custom color schemes to this array.
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
"name" : "Frost",
"background" : "#FFFFFF",
"black" : "#3C5712",
"blue" : "#17b2ff",
"brightBlack" : "#749B36",
"brightBlue" : "#27B2F6",
"brightCyan" : "#13A8C0",
"brightGreen" : "#89AF50",
"brightPurple" : "#F2A20A",
"brightRed" : "#F49B36",
"brightWhite" : "#741274",
"brightYellow" : "#991070",
"cyan" : "#3C96A6",
"foreground" : "#000000",
"green" : "#6AAE08",
"purple" : "#991070",
"red" : "#8D0C0C",
"white" : "#6E386E",
"yellow" : "#991070"
}
],

// Add custom keybindings to this array.
// To unbind a key combination from your defaults.json, set the command to "unbound".
// To learn more about keybindings, visit https://aka.ms/terminal-keybindings
"keybindings":
[
// 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+delete"}
]
}

最终效果如下:

Fluent Terminal

我最先使用的是这款,在github上搜索,然后下载后用ps运行文件夹下的Install.ps1即可,效果如下:

比起PS来已经美观多了,不过吹毛求疵的我还是感觉到了一丝丝不爽:

  1. 启动时有时会加载不出左侧的命令提示符,或者比较慢,需要按下回车才能出来,总之初始化有时会有点卡
  2. 字体稍微有点模糊,锯齿略明显(在字体小的情况下),而且显示效果不舒服
  3. 设置项较少,跟不上我装X追求完美的步伐
  4. 复制粘贴时有点问题,有时候只能整行复制,选取不到某段字符

ConEmu

官网下载安装即可

界面配置

设置默认以PS启动

设置字体

设置背景图片

设置PS为默认shell

设置配色

使用colortool来进行设置,colortool是微软出的一个powershell配色工具,支持Iterm主题

安装

1
scoop install colortool

待续

颜色设置

PS的字体颜色中有许多很暗的颜色的,比如在输入-xxxx选项是黑灰色,分辨度很差,我们可以自己修改成想要的颜色,点击”settings”,转到颜色选项卡

选择自己喜欢的颜色方案

然后下面第8个一般是暗色,这个就是控制上述选项的(我是和我shell中的颜色对比确定的),我们选择一个自己喜欢的颜色,效果如下,清楚了许多

其它颜色修改也可以参照上面的方法自己DIY,最后记得要保存成自己的颜色方案

效果展示

cmder(该软件也有丰富的设置项,只是我没怎么搞而已,也是一个不错的软件)

Fluent Terminal

ConEmu


https://sspai.com/post/52868
https://iterm2colorschemes.com/
https://github.com/JanDeDobbeleer/oh-my-posh